(二学结构体)
结构体类型定义
结构体是一批数据结合而成的新数据的类型。(组成的结构型数据可以有不同的数据类型)
一般形式:
struct 结构体名
{
数据类型1 成员名1;
数据类型2 成员名2;
数据类型3 成员名3;
...
};
如:
struct student
{
int number; //学号
char name[20]; //姓名
char sex; //性别
float score[7]; //成绩
};
Tips:
1.注意括号后的分号必不可少!
2.结构体名必须符合标识符的命名规则。
3.成员名有*,指针型 有[n],数组型
结构体变量的定义
1.先定义结构体,再定义结构变量
struct student
{
int number; //学号
char name[20]; //姓名
float score[7]; //成绩
char sex; //性别
};struct student stu1,stu2;
赋初值格式{成员1,成员2,3,4,....}
struct student stu1={001,"xiaoming",{80,90,50},'m'};
struct student stu2={002,"xiaogong",{40,50,60},'f'};
2.定义结构体时同时定义变量。
struct student
{
int number;
char name[20];
float score[7];
char sex;
}stu1={001,"xiaoming",{80,90,50},'m'};
stu2={002,"xiaogong",{40,50,60},'f'};
3.直接定义结构体类型变量。
struct
{
int number;
char name[20];
float score[7];
char sex;
}stu1={001,"xiaoming",{80,90,50},'m'}; //定义结构体变量并赋值
stu2={002,"xiaogong",{40,50,60},'f'};
结构体变量的引用
结构体名.成员名
结构体类型定义
结构体是一批数据结合而成的新数据的类型。(组成的结构型数据可以有不同的数据类型)
一般形式:
struct 结构体名
{
数据类型1 成员名1;
数据类型2 成员名2;
数据类型3 成员名3;
...
};
如:
struct student
{
int number; //学号
char name[20]; //姓名
char sex; //性别
float score[7]; //成绩
};
Tips:
1.注意括号后的分号必不可少!
2.结构体名必须符合标识符的命名规则。
3.成员名有*,指针型 有[n],数组型
结构体变量的定义
1.先定义结构体,再定义结构变量
struct student
{
int number; //学号
char name[20]; //姓名
float score[7]; //成绩
char sex; //性别
};struct student stu1,stu2;
赋初值格式{成员1,成员2,3,4,....}
struct student stu1={001,"xiaoming",{80,90,50},'m'};
struct student stu2={002,"xiaogong",{40,50,60},'f'};
2.定义结构体时同时定义变量。
struct student
{
int number;
char name[20];
float score[7];
char sex;
}stu1={001,"xiaoming",{80,90,50},'m'};
stu2={002,"xiaogong",{40,50,60},'f'};
3.直接定义结构体类型变量。
struct
{
int number;
char name[20];
float score[7];
char sex;
}stu1={001,"xiaoming",{80,90,50},'m'}; //定义结构体变量并赋值
stu2={002,"xiaogong",{40,50,60},'f'};
结构体变量的引用
结构体名.成员名