Are void and char pointer guaranteed to have the same memory representation?
In other words, after executing the following:
char a = 'z';
void *b = &a;
char *c;
memcpy(&c, &b, sizeof(b));
Can I be certain that *c == 'z' (there is no undefined behavior, etc.)?
Yes, according to the C99 standard (ISO/IEC 9899) section §6.2.5 point 27:
A pointer to
voidshall have the same representation and alignment requirements as a pointer to a character type.39)39) The same representation and alignment requirements are meant to imply interchangeability as arguments to functions, return values from functions, and members of unions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With