Hi,
how do i convert char* to const char* in the following example?
Code:UCHAR ucar[3];
ucar[0]='a';
ucar[1]='b';
ucar[2]='c';
//convert char* to const char*
//const char* car;
printf(car);
Printable View
Hi,
how do i convert char* to const char* in the following example?
Code:UCHAR ucar[3];
ucar[0]='a';
ucar[1]='b';
ucar[2]='c';
//convert char* to const char*
//const char* car;
printf(car);
-------------------------Method 1------------------------
Declare your array as CHAR (recommended)
-------------------------Method 2-------------------------
Cast to (char*) ---> printf((char*)ucar);
You don't need to cast something from type to const type because if you pass type to a function expecting a const type, it will automatically be upgraded.
If you have a const type and need to pass it to a function that isn't const-correct, you can cast it like Elysia suggested.
Unfortunately, this is unsigned char* and printf expects char*, thus causing a compile error in C++ and a warning in C.
Shouldn't matter, but does matter since it invokes a warning. Thus, a cast or change of types would be the best solution - compilers output warnings for a reason and it's best to satisfy the compiler by solving them.
The lack of a \0 in the string is another problem to be addressed.
I just saw that now. That would be why we usually do
OrCode:UCHAR ucar[] = "abc";
Though I'm not suer if "abc" will be considered just const char* and require a cast to unsigned const char*.Code:UCHAR uchar[4];
strcpy((char*)uchar, "abc");
Usually, unsigned char is typically used for buffers; when you use strings, normal char is what you use.