在C中,定义一个结构,可以有一下几种
1.先定义结构体,再定义变量,这种方式不能省略struct关键字
struct student
{
int age;
char* name;
};
struct student st1,st2;
2.定义结构体的同时定义变量
struct student
{
int age;
char*name;
}st1,st2;
3.直接定义变量,这种定义方式下,不能后续定义其他诸如st3,st4这样的变量
struct
{
int age;
char *name;
}st1,st2;
4.用typedef定义结构,这个方法相比第一种方式,在定义变量的时候省略了struct关键字
typedef struct
{
int age;
char*name;
}student;
student st1,st2;
5.用typedef定义一个别名,给结构体student定义一个别名mystu,后面可以用mystu定义变量
typedef struct student
{
int age;
char*name;
}mystu;
mystu st1;
6.用宏定义构体的成员,这种方式实际上可以应用在上面的几种方式里面,简而言之就是结构体的成员定义的时候用宏来实现
#define STUDENT int age;\
char* name;
struct student
{
STUDENT
};
struct student