C语言 结构体 struct和typedef struct在c语言中的用法
如果没有用到typedef,例如定义
struct test1
{
int a;
int b;
int c;
};
test1 t;//声明变量
上面语句就会报错。
声明变量时候就要用struct test1;这样就解决了
但如果这样定义的话
typedef struct test3
{
int a;
int b;
int c;
}test4;
test3 d; test4 f; //声明变量
这时候也会报错。
所以要struct test3这样来声明变量d;
分析一下:
上面的是标识符,test4 是变量类型(相当于(int,char等))。
我们可以用struct test3 d来定义变量d;为什么用test3 d来定义是错误的,因为test3相当于标识符,不是一个结构体,struc test3 合在一起才代表是一个结构类型。
所以声明时候要test3时候要用struct test3 d;
typedef其实是为这个结构体起了一个新的名字,test4;
typedef struct test3 test4;
test4 相当于struct test3;
还没有评论,来说两句吧...