关于 typedef & typedef struct & typedef union理解 --写给不长脑子的我
(1) struct{ int x; int y; }test1; 好,定义了 结构 test1,test1.x 和 test1.y 可以在语句里用了。(2) struct test {int x; int y; }test1; 好,定义了 结构 test1,test1.x 和 test1.y 可以在语句里用了。与 1 比,省写 了 test(3) typedef struct test {int x; int y; // 你漏打分号,给你添上 }text1,text2; 只说了 这种结构 的(类型)别名 叫 text1 或叫 text2真正在语句里用,还要写:text1 test1;然后好用 test1.x test1.y或写 text2 test1;然后好用 test1.x test1.y(4)type struct {int x; int y; }test1;这个不可以。改 typedef ... 就可以了。但也同 (3)一样,还要 写:test1 my_st;才能用 my_st.x 和 my_st.y
typedef union
问题2:
#include <stdio.h> typedef union {long i; int k[5]; char c; }DATE; struct date { int cat; DATE cow; double dog; }too; DATE max; main() {printf("%d\n",sizeof(struct date)+sizeof(max));} 程序结果为52,搞不懂……希望能给出详细解题过程!!!int k[5]是占几个字节呀??20还是10呀??
union是公用的,所以DATA的大小是int k[5] =4*5 = 20struct 是自己用自己的,所以大小是4+20+8 = 32结果就是52