关于 typedef & typedef struct & typedef union理解 --写给不长脑子的我

时间:2023-03-09 00:45:00
关于 typedef & typedef struct & typedef union理解 --写给不长脑子的我
写作原由,今晚再次查了typedef用法,就在这用着查着中做着一个个项目,可我还是记不住;脑子里装得是什么? 关于 typedef & typedef struct & typedef union理解 --写给不长脑子的我
typedef    struct
问题1:
请高手帮忙解释以下几种结构体定义的区别:
struct{
int x;
int y;
}test1; struct test
{int x;
int y;
}test1; typedef struct test
{int x;
int y
}text1,text2;
这几种方法把小弟弄得头疼,不胜感激!
 
 
 
(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 = 20

struct 是自己用自己的,所以大小是4+20+8 = 32

结果就是52