简体   繁体   中英

C Guarantees Re: Pointers to Void and Character Types, Typecasting

My best-effort reading of the C specification (C99, primarily) makes me think that it is valid to cast (or implicitly convert, where void * 's implicit conversion behavior applies), between any of these types:

void * , char * , signed char * , unsigned char *

I expect that this will trigger no undefined behavior, and that those pointers are guaranteed to have the same underlying representation.

Consequently, it should be possible to take a pointer of either one of those four types that is already pointing to an address which can be legally dereferenced, typecast and/or assign it to one of the three char type pointers, and dereference it to access the same memory, with the only difference being whether your code will treat the data at that location as a char , signed char , or unsigned char .

Is this correct? Is there any version of the C standard (lack of void * type in pre-standardization C not withstanding) where this is not true?

PS I believe that this question is answered piecemeal in passing in a lot of other questions, but I've never seen a single clear answer where this is explicitly stated/confirmed.

Consequently, it should be possible to take a pointer of either one of those four types that is already pointing to an address which can be legally dereferenced, typecast and/or assign it to one of the three char type pointers, and dereference it to access the same memory, with the only difference being whether your code will treat the data at that location as a char, signed char, or unsigned char.

This is correct. In fact you could take a valid pointer to an object of any type and convert it to some of those three and access the memory.

You correctly mention the provision about void * and char * etc. having the same representation and alignment requirements, but that actually does not matter. That refers to the properties of the pointer itself, not the properties of the objects being pointed to.

The strict aliasing rule is not violated because that contains an explicit provision that a character type may be used to read or write any object.

Note that if we have for example, signed char ch = -2; , or any other negative value, then (unsigned char)ch may differ from *(unsigned char *)&ch . On a system with 8-bit characters, the former is guaranteed to be 254 but the latter could be 254 , 253 , or 130 depending on the numbering system in use.

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