结构体的四种定义方法

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

struct student{

int data;

};

struct student stu1;//stu1为student的结构体变量

2.定义结构体变量的同时,定义结构体变量

struct student{

int data;

}stu1;

如果想要继续定义结构体变量

struct student stu2;//这样既可以再次定义结构体变量

3.不定义结构体类型,而直接定义结构体变量

struct{

int data;

}stu1;

这样做的缺陷很大,这样的话我们就不能再次定义stu1该类型的结构体变量了,可移植性非常差,也不灵活。

4.用typedef 来定义结构体变量及类型

typedef sturct node{

int data;

}Binode;

这样定义的话,结构体类型的名字就有了两个分别为node和Binode

自然定义结构体变量的方式也就有了两种

1.struct node val1

2.Binode val2

我在写代码时一般采用该种方法,个人喜好。

猜你喜欢

转载自www.cnblogs.com/zoutingrong/p/12199382.html