Hello, everyone. I have met a problem when I was trying compiling the next code.
Code:
#include <stdio.h>
typedef union {
short s[2];
int i[1];
}foo;
int main()
{
foo f;
short *a = f.s;
int cnt = 0x22222222;;
f.s[0]=0x1111;
f.s[1]=0x1111;
printf("%x %x\n", a[0], a[1]);
printf("%x %x\n", f.s[0], f.s[1]);
printf("\n");
*f.i = cnt;
printf("%x %x\n", a[0], a[1]);
printf("%x %x\n", f.s[0], f.s[1]);
printf("\n");
printf("%x %x\n", a[0], a[1]);
printf("%x %x\n", f.s[0], f.s[1]);
printf("\n");
return 0;
}
The compiler is gcc 3.2.3. When I used options -O. I got the result which I expected:
1111 1111
1111 1111
2222 2222
2222 2222
2222 2222
2222 2222
But when I used options -O2, I got the next result:
1111 1111
1111 1111
1111 1111
2222 2222
2222 2222
2222 2222
Could you explain where is the problem in the code?
Thanks for your help.