Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 919
martinkunev Avatar asked Mar 31 '26 04:03

martinkunev


1 Answers

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.

like image 92
Marco Bonelli Avatar answered Apr 02 '26 02:04

Marco Bonelli