在C中:
typedef struct Student{
int a;
}Stu;
struct Student stu1;
Stu stu2;
stu1.a = 2;
stu2.a = 3;
使用typedef后,Stu和struct Student一样,是一种结构体类型,能用来定义变量。
struct Student {
int a;
}Stu;
在C中,不加typedef时,只能用struct Student定义。此时的 Stu 是一个变量,不是一个类型。
在C++中:
typedef struct Student{
int a;
}Stu;
在C++中,只写struct也可以实现和C中typedef struct一样的功能。上面代码块中的写法,使用Student、struct Student、Stu都可以定义变量。
struct Student{
int a;
}Stu;
在上面这个代码块里,没有写typedef时,仍然可以用struct Student和Student来定义变量,但Stu此时只是一个结构体变量,不是类型。