在《数据结构》C语言版中经常遇到定义结构体不同的方式:
typedef struct和struct。它们之间的区别如下:
typedef struct student{
int id;
char name;
}stu;
其中student为结构名,stu为对象名。typedef即给struct取别名,在C中定义结构体需要用typedef。此时stu == struct student。在声明变量时只需要写stu stu1。如果不用typedef则只能通过struct student stu1声明变量。
struct student{
int id;
char name;
}
在C++中定义结构体可直接使用struct,但在声明struct新对象时,必须带上struct,即struct 结构名 对象名,比较麻烦,因而写代码时更多的是为struct取别名,写为typedef。