I've been reading some articles about void* type pointers and found this requirement from the Standard.
6.2.5.27:
A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.39) Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements.
I see that the Standard does not guarantee all pointer types have the same length, so the bottom line here is that a void* pointer has the same length and alignment rules as char*, right?
What I don't get is the footnote 39), which says
The same representation and alignment requirements are meant to imply interchangeability as arguments to functions, return values from functions, and members of unions.
My questions are:
What does it mean by "interchangeability"? Does it say the argument and the return values of a function
void* Func(void*)can both bechar*?If so, is it an implicit conversion made by the compiler?
And what is it about the members of unions? I really don't get a grasp of the meaning of this. Can anyone give me a simple example?
void*and back without a cast. That is the basis for the interchangeability.No implicit conversion takes place, avoid*pointer is simply a pointer without a specific type. And, since type controls pointer arithmetic, you can't do pointer arithmetic onvoid*pointers. Just like you cannot dereference avoid*pointer because the type information is missing -- it would be an incomplete type. So you have to assign or cast a void pointer before dreferencing. - David C. Rankinvoid*andchar*having the same representation.void*doesn't have to have the same representation or alignment as other pointer types. - John Kugelmanchar*exception) I haven't done a critical interpretation of the standard section, just more a practical discussion of how that section doesn't hold any hidden gotchas. - David C. Rankinvoid *foo(void *p)in one translation unit and declarechar *foo(char *p)in another, and call it using the latter, and it would work because the types are interchangeable. But it is undefined according to the normative text of the C standard, aside from this passage in C 2018 6.2.5 28 about the same representation. - Eric Postpischil