struct和typedef struct
在C里
typedef struct Student
{
int a;
}Stu;
于是在声明变量的时候就可:
Stu stu1;
这里的Stu实际上就是struct Student的别名。
Stu==struct Student
另外这里也可以不写Student(于是也不能struct Student stu1;了,就必须是Stu stu1;)
typedef struct
{
int a;
}Stu;
在c++里
struct Student
{
int a;
};
于是就定义了结构体类型Student,声明变量时直接
Student stu2;
在c++中如果用typedef的话,又会造成区别:
struct Student
{
int a;
}stu1;//stu1是一个变量
typedef struct Student2
{
int a;
}stu2;//stu2是一个结构体类型=struct Student
使用时可以直接访问
stu1.a=10
但是stu2则必须
stu2 s2;
s2.a=10;
typedef struct
{
int num;
int age;
}aaa,bbb,ccc;
这相当于
typedef struct
{
int num;
int age;
}aaa;
typedef aaa bbb;
typedef aaa ccc;
也就是说aaa,bbb,ccc三者都是结构体类型。声明变量时用任何一个都可以,在c++中也是如此。