c++union
a)
#i nclude <stdio.h>
union
{
int i;
char x[2];
}a;
void?main()
{
a.x[0] = 10;
a.x[1] = 1;
printf("%d",a.i);
}
答案:266 (低位低地址,高位高地址,內存占用情況是Ox010A)
b)
???? main()
???? {
????????? union{??????????????????
?????????????? int i;
?????????????? struct{????????????
??????????????????? char first;
??????????????????? char second;
?????????????? }half;
????????? }number;
????????? number.i=0x4241;????????
????????? printf("%c%c/n", number.half.first, mumber.half.second);
????????? number.half.first='a';??
????????? number.half.second='b';
????????? printf("%x/n", number.i);
????????? getch();
???? }
答案: AB?? (0x41對應'A',是低位;Ox42對應'B',是高位)
???????6261 (number.i和number.half共用一塊地址空間)
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
- 上一篇: static变量初始化
- 下一篇: 删除c++程序中的注释