xsari3x
xsari3x

Reputation: 452

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

This isn't working in visual studio 2010 , it gives me the following error

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

}

Edit 1: My question is why this works on Embedded systems, but doesn't work on PC?

enter image description here

But when I change it to :

 char* test = "ATGST";  

it works.

The main thing that I write code for embedded systems using C, and I use visual studio to test some functions so I don't have to test it in real time on a Micro-controller.

I need an explanation, because Micro-controllers accepts the first code.

Upvotes: 6

Views: 12127

Answers (5)

user1309389
user1309389

Reputation:

Edited to conform to the removal of the C++ tag and to appease the embedded tag.

First, the problem at hand, you are trying to pass a char[] literal into an unsigned char*. You can't really equate char with either unsigned or signed, it is a bit special in that regard. Also, a string literal is given unique storage and should never be modified. If you're dealing with characters, you need to use a standard char* in which char[] can decay into. You could forcefully cast it, but I don't like to recommend such things. It is safe to do, as one of the comments pointed out. Actually, it is actually one of the rare things that are really a safety no-brainer.

But there is far too little space for a tight answer to provide enough qualification on reinterpret_casting, which is basically saying to the compiler that you know what you're doing. That is potentially very dangerous and should only be done when you're quite sure about the problem at hand. The char is usually just generic, not even signed or unsigned. Since an unsigned char has a bigger range than a char and usually char uses the positive subset of the signed char to describe characters (or any other kind of data that can fit), if your data is not in the extended positive range, you're good to go. But, do conform to the environment and code safely.

On the entry point function - conforming edit

Since it has been established that you work on an embedded system, this implies that your program is very likely not required to return anything, so it can remain void main() (it could also be the case that it requires very different returns specified by the given embedded system, the OP knows the most about the requirements his system imposes). In a lot of cases, the reason you can remain with void is because there is no environment/OS to appease, nobody to communicate with. But embedded systems can also be quite specialized and it is best to approach by studying the given platform in detail in order to satisfy the requirements imposed (if any).

Upvotes: 12

Kinjal Patel
Kinjal Patel

Reputation: 405

I believe this is the case of assigning string to unsigned char *.

Well, when you assign a string value, it will assign ASCII values associated with characters, so you should use char * in place of unsigned char *.

if you want to assign values other than strings, characters then your implementation is correct.

hope it helps.

Upvotes: 3

Lundin
Lundin

Reputation: 213902

My question is why this works on Embedded systems, but doesn't work on PC?

Most likely because you are accidentally compiling the PC code in C++ which has stricter type checking than C. In C, it doesn't matter the slightest whether you use unsigned char or plain char, the code will compile just fine.

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

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

}

Upvotes: 0

John Riselvato
John Riselvato

Reputation: 12914

If you are using character types for text, use the unqualified char.

If you are using character types as numbers, use unsigned char. unsigned char, which gives you at least the 0 to 255 range.

for more information: What is an unsigned char?

Upvotes: 0

Puppy
Puppy

Reputation: 146940

For one, you need a const in there. And secondly, char != unsigned char, and also (uniquely) != signed char.

String literals are of type const char[N]- for an appropriate size N, and therefore can only be converted to a const char*. Note that the language has a special rule allowing you to implicitly drop the const but it's still UB to modify a string literal, making it a terribly bad idea to do so.

The micro-controller's C implementation is non-conforming in this regard. It would be better to simply use const char*, as is correct, rather than try to hack VS into accepting incorrect code.

Upvotes: 5

Related Questions