1.语言方面
C语言中,两种方式在声明变量时存在区别
若使用typedef struct定义结构体类型,则在声明变量时可以是:Student stu;
typedef stuct Student{
int a;
}
Student stu;
若直接使用struct,则必须在声明变量时使用:struct Student stu;
stuct Student{
int a;
}
stuct Student stu;
在Cpp中,直接使用struct,则在声明变量时可以使用:Student stu;(因为C++中,struct也是一种类)
stuct Student{
int a;
}
Student stu;
2.C++中使用typedef与不使用的区别
简单来说,使用typedef stuct定义的是一个结构体类型,而直接使用struct定义的是一个变量。
用一个例子来说明:
struct Student1
{
int a;
}stu1;//stu1是一个变量
typedef struct Student2
{
int a;
}stu2;//stu2是一个结构体类型
直接使用struct时,stu1是一个结构变量,在使用时可以直接访问stu1.a,此时是直接当成成员函数使用;
但是,使用typedef struct时,stu2是一个结构别名,stu2则必须要先stu2 s2,然后使用s2,如s2.a=10。
若还是不明白,下面的链接有更详细的说明:
struct与typedef struct的区别