1.struct关键词
- 它将一些相关联的数据聚合成一个整体,方便使用。
struct{
char name[20];
int gender;
double height;
double weight;
}
//以上被称作结构,是一个数据类型!
//和基本数据类型声明一样
2.生成聚合数据并使用
struct{
char name[20];
int gender;
double height;
double weight;
}arr[10];
//声明了一个数组
//访问聚合整体中的每一项成员
arr[0].字段名;
arr[0].name;
arr[0].gender;
scanf{"%s %d %lf %lf",arr[0].name,&arr.gender,
&arr.height,&arr.weight};
3.结构别名
struct 结构别名{
char name[20];
int gender;
double height;
double weight;
}变量名;
//如果在这之后还要使用这个结构:
struct 结构别名 变量名;
struct person{
char name[20];
int gender;
double height;
double weight;
}message;
struct person staff;
struct person crew;
//或者:
struct person{
char name[20];
int gender;
double height;
double weight;
};
struct person message;
struct person staff;
struct person crew;
如果结构类型声明在一个函数中,那么别名也只能在这个函数内部使用
4.初始化结构
- 初始化列表由花括号包括
- 花括号内为结构成员需要被初始化的值
- 初始化的值按照结构成员声明时的顺序依次排列
- 每个初始化值之间用逗号隔开
#include<stdio.h>
int main(){
struct person{
char name[20];
int gender;
double height;
double weight;
};
struct person message={"丹",1,156,42.5};
printf("%s %d %.2lf %.2lf",message.name,message.gender,
message.height,message.weight);
return 0;
}
5.定义结构数组
#include<stdio.h>
int main() {
struct person {
char name[20];
int gender;
double height;
double weight;
};
struct person message[3] = {
{"李四",2,188,67.8},
{"张三",2,186,69.2},
{"翠花",1,168,52}
};
for (int i = 0; i < 3; i++) {
struct person per = message[i];
printf("%s\t%d\t%.2lf\t%.2lf\t\n",
per.name, per.gender, per.height, per.weight);
}
return 0;
}
6.嵌套结构
#include<stdio.h>
int main() {
struct contact {
char phone[20];
char email[20];
};
struct person {
char name[20];
int gender;
double height;
double weight;
struct contact c;
};
struct person message[3] = {
{"李四",2,188,67.8,{"13567890654","242717324@qq.com"}},
{"张三",2,186,69.2,{"17867890654","247637324@qq.com"}},
{"翠花",1,168,52,{"13123890654","244567324@qq.com"}}
};
for (int i = 0; i < 3; i++) {
struct person per = message[i];
printf("%s\t%d\t%.2lf\t%.2lf\t%s\t%s\t\n",
per.name,per.gender, per.height, per.weight,
per.c.phone,per.c.email);
}
return 0;
}
7.指向结构的指针
#include<stdio.h>
int main() {
struct person {
char name[20];
int gender;
double height;
double weight;
};
struct person message = {"李四",2,188,67.8};
struct person* p = &message;
printf("%s\t%d\t%.2lf\t%.2lf\t",
(*p).name, (*p).gender, (*p).height, (*p).weight);
return 0;
}
运行结果:
#include<stdio.h>
int main() {
struct contact {
char phone[20];
char email[20];
};
struct person {
char name[20];
int gender;
double height;
double weight;
struct contact c;
};
struct person message[3] = {
{"李四",2,188,67.8,{"13567890654","242717324@qq.com"}},
{"张三",2,186,69.2,{"17867890654","247637324@qq.com"}},
{"翠花",1,168,52,{"13123890654","244567324@qq.com"}}
};
for (int i = 0; i < 3; i++) {
struct person* p = &message;
printf("%s\t%d\t%.2lf\t%.2lf\t%s\t%s\t\n",
(*(p+i)).name, (*(p + i)).gender, (*(p + i)).height, (*(p + i)).weight,
(*(p + i)).c.phone, (*(p + i)).c.email);
}
return 0;
}
//成员运算符点号.的优先级高于取值运算符*,所以要加()。
运行结果:
8.->运算符
C语言中提供了更加方便的运算符:成员间接运算符->
//使用成员间接运算符->
#include<stdio.h>
int main() {
struct person {
char name[20];
int gender;
double height;
double weight;
};
struct person message = {"李四",2,188,67.8};
struct person* p = &message;
printf("%s\t%d\t%.2lf\t%.2lf\t",
p->name, p->gender, p->height, p->weight);
return 0;
}
运行结果:
8.结构如何在函数中传递
#include<stdio.h>
struct person {
char name[20];
int gender;
double height;
double weight;
};
void change(struct person per) {//修改传入的参数
strcpy(per.name, "翠花");
per.gender = 1;
per.height = 168;
per.weight = 52;
}
int main() {
struct person message = { "张三",2,188,67.8 };
change(message);
struct person *p = &message;
printf("%s\t%d\t%.2lf\t%.2lf\t",
p->name, p->gender, p->height, p->weight);
return 0;
}
运行结果:为什么会导致这个结果?
因为实参message与形参per是相互独立的,所以函数change,并不能使message的值改变.那么应该怎么改这个代码呢?
我们可以将change函数的参数改成指向结构的指针:
#include<stdio.h>
struct person {
char name[20];
int gender;
double height;
double weight;
};
void change(struct person* per) {//修改传入的参数
strcpy(per->name, "翠花");
per->gender = 1;
per->height = 168;
per->weight = 52;
}
int main() {
struct person message = { "张三",2,188,67.8 };
change(&message);
struct person* p = &message;
printf("%s\t%d\t%.2lf\t%.2lf\t",
p->name, p->gender, p->height, p->weight);
return 0;
}
运行结果:
持续更新【C语言】系列!有需要的可以移步秃头程序媛主页!