|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
为什么输出时的数据于输入时的数据不服?
printf(" test.i: %d\n",test.i);
printf(" test.pi:%.2f\n",test.pi);
printf("test.str:%s\n",test.str);
- #include<stdio.h>
- #include<string.h>
- union Test
- {
- int i;
- double pi;
- char str[6];
- };
- int main(void)
- {
- union Test test;
- test.i=520;
- test.pi=3.14;
- strcpy(test.str,"FishC");
-
- printf("addr of test.i: %p\n",&test.i);
- printf("addr of test.pi:%p\n",&test.pi);
- printf("addr of test.str:%p\n",&test.str);
- printf(" test.i: %d\n",test.i);
- printf(" test.pi:%.2f\n",test.pi);
- printf("test.str:%s\n",test.str);
- return 0;
- }
复制代码
- $ ./main
- addr of test.i: 0xffffcc38
- addr of test.pi:0xffffcc38
- addr of test.str:0xffffcc38
- test.i: 1752394054
- test.pi:3.13
- test.str:FishC
复制代码
对 test 的最后一次赋值是 strcpy(test.str,"FishC");
所以 test.str:FishC 这个输出没问题,(共用体)union 就是共用一个位置,后一个覆盖前一个,只会保留最后一次赋值
|
|