I’m compiling a low level code using many bunch of bytes. In some case it is handy for me to define then using the double quote enclosed old C strings.
But when compiling with gcc or g++ (don’t know behavior with other compilers), it keeps bothering me with sign of pointed string.
Basically when I write this
const unsigned char & refok = *"ABCDEFGHI";
EDIT: ok, the code above is not really working as it will in theory just keep a reference to a copy of the first char of the string. It actually allow access to all the string with some compilers because of optimization, but may break any time.
or this
const unsigned char oktoo[10] =
{'A','B','C','D','E','F','G','H','I',0};
the compiler doesn’t say anything.
But it definitely reject this one:
const unsigned char * bad = "ABCDEFGHI";
with message
error: invalid conversion from
‘const char*’ to ‘const unsigned char*’
[-fpermissive]
It’s not even a warning, it’s an error.
I’m wondering why this one should be more of an issue than when using a reference, or converting individual chars from signed chars to unsigned chars ? Or am I missing something ?
I think you’re missing a lot of things!
The first line probably does something completely different from what you think. (It involves a conversion and extension-of-lifespan of a temporary.)
The second line initializes each unsigned char from the corresponding char in the brace initializer.
In the third line, the compiler is correct: the string literal has type
const char *, and you cannot convert aT*to aU*in general.Note that the standard demands explicitly that
char,unsigned charandsigned charbe distinct types. The reasoning here is thatcharshould be the platform’s native byte type, while the other two are explicitly unsigned and signed integral types. The unsigned/signed types are for algebraic operations, while the naked type is for interfacing with the system (e.g. command line arguments, and file I/O).