In the -fstrict-aliasing
dialect of gcc, even structures with the same tag name, whose members are identical, are alias-incompatible if they are declared in different scopes. The only way two structures-type lvalues within a compilation unit will be treated as alias-compatible is if both are based upon the same struct declaration. Structures whose types are declared identically but separately are not alias compatible in the gcc dialect.
Consider, for example:
void test_write(void *p)
{
struct foo { int x; };
struct foo *pp = p;
pp->x = 1;
}
struct foo { int x; };
int test_write_and_read(struct foo *p, int i, int j)
{
p[i].x = 2;
return p[j].x;
}
struct foo foo_arr[4];
int test(int i)
{
test_write_and_read(foo_arr, 0, 1);
test_write(foo_arr+i);
return test_write_and_read(foo_arr, 1, 0);
}
int (*volatile vtest)(int) = test;
#include <stdio.h>
int main(void)
{
int result = vtest(0);
printf("%d %d\n", result, foo_arr[0].x);
}
In the gcc dialect, even though the type struct foo
used within test_write has the same tag name and same members as the struct foo
type used elsewhere, gcc will not allow for the possibility that a write to pp->x
within test_write()
might affect the value of foo_arr[j].x
within test()
, even though the address foo_arr+j
is passed to that function.
Given that the gcc won't recognize any form of compatibility between the structures, despite their having the same tag name and identitical contents, one should not regard similarity between the contents of structures as an indication that gcc will accommodate the possibility of their being used interchangeably.