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);
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);
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.
The lack of a \0 in the string is another problem to be addressed.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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.