结构体的定义:
/*方式1*/
struct nature
{
char a;
int b;
flaot f;
}; //构造一个新的数据类型
struct nature stc;//定义了一个叫stc的该结构体类型数据变量
struct nature
{
char a;
int b;
flaot f;
}stc15; //构造一个新的数据类型并且同时定义了一个或多个类型变量(stc15只一个)
但是如果还是想再使用该结构体类型来定义变量依然是要 struct nature xx
/*方式2*/
typedef struct nature
{
char a;
int b;
flaot f;
}_nature; //构造一个结构体数据类型并且重新定义为_nature
_nature stm;
typedef struct nature
{
char a;
int b;
flaot f;
}avr,msp; //构造一个结构体数据类型并且重新定义为avr和msp
//avr 和msp可以认为是并联电压这种并行结构都是新的数据结构类型都是指向同一个struct类型
avr si; msp sx;
si.a = 1;si.b =2; si.f = 3.1;
sx.a =2; sx.b =3; sx.f = 4.3;//两个变量都是合法都能运行打印出结果
//不过这样做没什么意义
typedef struct nature
{
char a;
int b;
flaot f;
}avr,*msp; //构造一个结构体数据类型并且重新定义为avr和*msp
结构体的初始化
/*方式1*/
/*first*/
struct nature stc={1,2,3.3};//对整个变量的内部成员一起赋值
/*second*/
stc.a= 1,stc.b =2; stc.f = 3.3;//单独赋值
/*third*/
struct nature stm=
{
.a = 1;
.b = 2;
.f = 3.3;
}; //linux的乱序
/*fourth*/
_nature cc =
{
a: 1;
b: 2;
f: 3.3;
};
/*补充*/
struct nature
{
char a;
int b;
float f;
}avr={1,2,4.4},msp={3,5,6.6};//可行
结构体数组与指针
以上都是对单个个体的一个信息封装多个话则需要数组和指针以下就是结构体指针数组的基本操作
typedef struct student
{
char name[10];
int age;
float hight;
}stu;
stu Class[3] =
{ {"lixiaohua",18,130.5},
{"linjian",20,115.8},
{"chensong",21,120.6}
}; //一个班里的3个学生的信息
stu *person;
person = Class;
printf("%s,%d,%f\n",Class[0].name, Class[0].age, Class[0].hight);
printf("%s,%d,%f\n",(++person)->name,(person)->age,(person)->hight);
printf("%s,%d,%f\n", (++person)->name, (person)->age,(person)->hight);
//依次打印出三个人的信息