int main()
{
struct { int x; } foo;
dostuff(&foo);
return 0;
}
void dostuff(void *ptr)
{
struct { int x; } *p = ptr;
p->x = 5;
}
Dereferencing p
is a strict-aliasing violation because the two unnamed structs cannot alias each other for they are not compatible.
Now what problems could/would arise in such code?
Edit: I'm still not sure whether this defined behaviour since they don't have the same tag.
Assuming they are not compatible, would the following make any difference?
union u {
void *v;
struct {
int x;
} *p;
};
void dostuff(void *ptr)
{
union u tmp = {.v = ptr};
tmp.p->x = 5;
}