Search code examples
clanguage-lawyerc99void-pointers

Compatibility between void * and char *


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


Solution

  • 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.