Why can't I declare unsigned char * test = "Some text"

This does not work in visual studio 2010, it gives me the following error

void main (void)
{
     unsigned char* test = "ATGST"; 

}

Edit 1: My question is why does this work on embedded systems but not work on PC?

enter image description here

But when I change it to:

 char* test = "ATGST";  

it works.

The main thing is that I write code for embedded systems using C, and I use visual studio to test some functions, so I do not need to test it in real time on the Micro-controller.

I need an explanation because microcontrollers accept the first code.

+5
source share
5 answers

Edited to match the deletion of the C ++ tag and to please the inline tag.

-, , char[] unsigned char*. char unsigned signed, . , . , char*, char[] . , . , . , , .

, reinterpret_casting, , , . , . char , . unsigned char , char, char char ( , ), , . , .

-

, , , , , -, void main() ( , , , ). , void, , /, , . , , , ( ).

+10

const. -, char!= unsigned char, ()!= signed char.

const char[N] - N const char*. , , const, UB, , .

C . const char*, , VS .

+6

, unsigned char *.

, , ASCII, , char * unsigned char *.

, , , .

, .

+3

, char.

, unsigned char. unsigned char, 0 255.

: unsigned char?

0

, , ?

Most likely, because you accidentally compile PC code in C ++, which has a more stringent type check than C. In C, it doesn’t matter whether you use unsigned char or plain char, the code will compile just fine.

However, there are some problems with your code that needs to be fixed, as suggested in other answers. If the code should work on both the built-in and Windows, you should rewrite it as:

#ifdef _WIN32
int main (void)
#else
void main (void)
#endif
{
  const unsigned char* test = "ATGST"; 

}
0
source

All Articles