1. 先定义结构体类型,再定义结构体变量。

struct student{ char no[20]; //学号 char name[20]; //姓名 char sex[5]; //性别 int age; //年龄 };
struct student stu1,stu2; //此时stu1,stu2为student结构体变量 2. 定义结构体类型的同时定义结构体变量。

struct student{ char no[20]; //学号 char name[20]; //姓名 char sex[5]; //性别 int age; //年龄 } stu1,stu2;
此时还可以继续定义student结构体变量,如:

struct student stu3;

3、不指定类型名而直接定义结构体变量

struct{ char no[20]; //学号 char name[20]; //姓名 char sex[5]; //性别 int age; //年龄 } stu1,stu2;
一般不使用这种方法,因为直接定义结构体变量stu1、stu2之后,就不能再继续定义该类型的变量。

4、用typedef定义结构体变量

typedef struct stdudent

{ char name[20]; int age; }student_t; 上面的代码,定义了一个结构体变量类型,这个类型有2个名字:第一个名字是struct student;第二个类型名字是student_t.

定义了这个之后,下面有2中方法可以定义结构体变量

第一种: struct student student_1; //定义了一个student_1的结构体变量

第二种:student_t student_1 //定义了一个student_1的结构体变量

推荐在实际代码中使用第四种方法定义结构体变量。