Home > Software design >  Compatibility between void * and char *
Compatibility between void * and char *

Time:10-03

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.)?

CodePudding user response:

Yes, according to the C99 standard (ISO/IEC 9899) section §6.2.5 point 27:

A pointer to void shall 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.

  • Related