简体   繁体   中英

C str representation - const char* vs const unsigned char*

I asked this on Taking an index out of const char* argument along with another question, but debate got only to the first question, so I splitted it to a different thread.

Question:

Is there any reason anyone would ever want to represent a C string as and not as ? 而不是吗?

  • On one hand, I see the commonly used representation of C str as all the time.
  • On the other hand, using sometimes forces a cast to unsigned, like in the example linked above. 有时会强制强制转换为无符号,就像上面链接的示例一样。

Thanks,

Yes, of course a general read-only string should be const char * , since char (with unspecified implementation-specified signedness) is the default type for a character.

In other words, a literal like "foo" consists of char , not unsigned char , elements.

Of course you can interpret the characters as unsigned if you feel like it, but then you might need a cast.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM