day3-union.c
ここでの学習ポイントは
unionをつかった場合のアドレス
printfの表示format %08x\nで表示する際に(int*)でキャストしているのは
unionを構成している、char,short int, intの3つのなかで
intの大きさが一番大きいから
[hirasawa@ubunt1004-32-2 gcc-Programming-Kobo]$ cat myunion.c #include <stdio.h> int main(void) { union{ char a; short int b; int c; } example; printf("sizeofunit-example is %d\n",sizeof(example)); printf("address of example is %p\n",&example); printf("address of example.a is %p\n",&(example.a)); printf("address of example.b is %p\n",&(example.b)); printf("address of example.c is %p\n",&(example.c)); example.c = 0; printf("content of example is %08x\n",*((int*) &example)); example.a = 0x12; printf("content of example is %08x\n",*((int*) &example)); example.b = 0x1234; printf("content of example is %08x\n",*((int*) &example)); example.c = 0x12345678; printf("content of example is %08x\n",*((int*) &example)); } [hirasawa@ubunt1004-32-2 gcc-Programming-Kobo]$ [hirasawa@ubunt1004-32-2 gcc-Programming-Kobo]$ ./myunion sizeofunit-example is 4 address of example is 0xbfbaad9c address of example.a is 0xbfbaad9c address of example.b is 0xbfbaad9c address of example.c is 0xbfbaad9c content of example is 00000000 content of example is 00000012 content of example is 00001234 content of example is 12345678 [hirasawa@ubunt1004-32-2 gcc-Programming-Kobo]$
これがアッセンブラに変換されるときと想像してみてね、ってのが西田さんの意図なんだろうなぁ
example.a = 0x12をみたら、movb $0x12 , 0x0bfbaad9cを思い浮かべてね、ってことですね