typedef为类型定义,typedef struct是为了使用结构体方法
若struct list{}这样定义结构体,在申请list变量时,需要这样写,struct list n;
若用typedef,则可以typedef struct list{}LIST;,在申请变量时就可以这样写 LIST n;可以省去struct这个关键字。
1.在c中定义结构体类型要用typedef;
typedef struct student
{
int a;
}S;
这样在声明变量时就可以用S s1,如果没有typedef就必须用struct sudent s1来声明,这里的S实际就是struct student的别名。
还有一种方式就是可以省略student
typedef struct
{
int a;
}S;这样就不可能用struct student s1;了。
但在C++中很简单
struct student
{
int a;
};
这样就定义了结构体类型student,声明变量时直接student s1
2.在c++中如果用的话,又会造成区别
struct student
{
int a;
}Stu1;//Stu是一个变量
typedef struct student
{
int a;
}stu2;//stu是一个结构体类型
使用时可以直接访问stu1.a
但是stu2则必须先stu2 s2,s2.a=10;