Incorrect conversion from 'const char * to' unsigned char *

Simple C ++ code:

int main(){ unsigned char* t="123"; } 

when compiling with g ++, it gives the following error:

 invalid conversion from 'const char*' to 'unsigned char*' [-fpermissive] 

Why?

+8
c ++ casting type-conversion
source share
4 answers

In C ++, string literals have types of constant character arrays. For example, the string literal "123" is of type const char[4] .

In expressions with rare exceptions, arrays are converted to pointers to their first elements.

So in this ad

 unsigned char* t="123"; 

The initializer is of type const char * . There is no implicit conversion from const char * to unsigned char *

You can write

 const unsigned char* t = reinterpret_cast<const unsigned char *>( "123" ); 
+13
source share

Another approach that gets you a modifiable array of unsigned char , as you originally wanted, is:

 #include <cstdlib> #include <iostream> using std::cout; using std::endl; int main() { unsigned char ta[] = "123"; unsigned char* t = ta; cout << t << endl; // Or ta. return EXIT_SUCCESS; } 

You can add const to both declarations, if you want, get a const unsigned char without an explicit cast.

+3
source share

Just use

  • just char * instead of unsigned char * during declaration

  • char t [MAX_SIZE] = "123"; // MAX _SIZE must be defined earlier

  • time-tested strcpy () and strncpy functions

0
source share

Conversions from one type to another are easily performed using self-defined macros. So, here is a set of macros that you can use on any platform (Windows, Linux, Solaris, AIX, etc.)

 #define M_ToCharPtr(p) reinterpret_cast<char*>(p) // Cast to char* #define M_ToWCharPtr(p) reinterpret_cast<wchar_t*>(p) // Cast to wchar_t* #define M_ToConstCharPtr(p) reinterpret_cast<const char*>(p) // Cast to const char* #define M_ToConstWCharPtr(p) reinterpret_cast<const wchar_t*>(p) // Cast to const wchar_t* #define M_ToUCharPtr(p) reinterpret_cast<unsigned char*>(p) // Cast to unsigned char* #define M_ToConstUCharPtr(p) reinterpret_cast<const unsigned char*>(p) // Cast to const unsigned char* #define M_ToUCharPtr(n) reinterpret_cast<unsigned char*>(n) // Cast to unsigned char* #define M_ToVoidPtr(p) reinterpret_cast<void*>(p) // Cast to void* #define M_ToConstVoidPtr(p) reinterpret_cast<const void*>(p) // Cast to const void* #define M_ToIntPtr(n) reinterpret_cast<int*>(n) // Cast to int* #define M_ToConstIntPtr(p) reinterpret_cast<const int*>(p) // Cast to const int* #define M_ToDoublePtr(n) reinterpret_cast<double*>(n) // Cast to double* #define M_ToConstDoublePtr(n) reinterpret_cast<const double*>(n) // Cast to const double* #define M_ToBoolPtr(n) reinterpret_cast<bool*>(n) // Cast to bool* #define M_ToConstBoolPtr(n) reinterpret_cast<const bool*>(n) // Cast to const bool* // General Cast #define M_To(T, p) reinterpret_cast<T>(p) // Cast to T 

In your case

 const unsigned char* t = reinterpret_cast<const unsigned char *>("UCHAR TO CONST UCHAR"); 

equivalently

 const unsigned char* t = M_ToConstUCharPtr("UCHAR TO CONST UCHAR"); 
-one
source share

All Articles