一、在C中定义一个结构体类型要用typedef
1、
typedef struct tagStudent
{
int a;
}Stu;
Stu实际上就是struct tagStudent的别名,声明变量可以 struct tagStudent stu1; 或者Stu stu2;
2、
typedef struct
{
int a;
}Stu;
声明变量只能是Stu stu1;
二、在C++中可以简单地
1、
struct Student
{
int a;
};
Student 是结构体类型,可以直接声明变量,Student stu1;
2、
struct Student
{
int a;
}stu2;//stu1是一个变量
Student 是结构体类型,可以直接声明变量,Student stu1; stu2已经是一个变量;
3、typedef struct tagStudent
{
int a;
}Stu,*pStu;
pStu 与typedef struct tagStudent* pStudent 中的pStudent是等价的。类似 typedef int* p;
表示定义一种数据类型,该类型是指向结构体的,pStu pstu1声明了pstu1是一个指向结构体的指针;使用如下:
pStu pstu1;
pstu1 = malloc(sizeof(Stu));
pstu1->a=20;