Can I be sure that the following code will work on all platforms?
struct example{
int a;
int b;
} example;
*((int*)(((void*)&example) + sizeof(int))) = 33;
This should change the value of (b) inside (example) to 33.
Can I be sure that the following code will work on all platforms?
struct example{
int a;
int b;
} example;
*((int*)(((void*)&example) + sizeof(int))) = 33;
This should change the value of (b) inside (example) to 33.
It will not for sure.
&example + sizeof(int)
this operation moves the pointer sizeof(int) * sizeof(example)
bytes ahead.
And this line will not compile at all
*(&example + sizeof(int)) = 33;
To know the offset of the particular field in the struct or union use offsetof