1. structure体overview
1.1 what is structure体
structure体 is auser定义 dataclass型, 它允许将不同class型 data成员组合 in 一起, 形成一个 new 复合dataclass型. structure体可以package含不同class型 成员, such as整数, 浮点数, 字符, array, 指针etc., 甚至可以package含otherstructure体.
structure体 主要用途including:
- 组织相关 data项
- 表示 complex datastructure
- improvingcode readable 性 and 可maintenance性
- implementation面向objectprogramming 一些features
1.2 structure体 声明
in Clanguagein, 声明structure体 basic语法such as under :
struct structure体名 {
dataclass型 成员1;
dataclass型 成员2;
// 更 many 成员...
};
// example: 声明一个表示学生 structure体
struct Student {
char name[50];
int age;
float score;
};
注意: structure体声明以分号结尾, 这 is a common error点.
2. structure体variable 定义 and 初始化
2.1 structure体variable 定义
定义structure体variable 方式 has 两种:
2.1.1 in 声明structure体 after 定义
// 声明structure体
struct Student {
char name[50];
int age;
float score;
};
// 定义structure体variable
struct Student stu1, stu2;
2.1.2 in 声明structure体 同时定义
// 声明structure体 同时定义variable
struct Student {
char name[50];
int age;
float score;
} stu1, stu2;
2.2 structure体variable 初始化
structure体variable可以 in 定义时for初始化, using花括号括起来 初始化list:
// 声明structure体
struct Student {
char name[50];
int age;
float score;
};
// 初始化structure体variable
struct Student stu1 = {"张三", 18, 95.5};
// 部分初始化, 未初始化 成员会被自动设置 for 0
struct Student stu2 = {"李四", 19};
in C99及以 after 标准in, 还可以using指定初始化器来初始化structure体成员:
// using指定初始化器
struct Student stu3 = {
.name = "王五",
.score = 92.0
}; // age会被自动设置 for 0
3. structure体成员 访问
3.1 using点运算符(.)访问成员
for 于普通 structure体variable, using点运算符(.)来访问其成员:
#include <stdio.h>
// 声明structure体
struct Student {
char name[50];
int age;
float score;
};
int main() {
// 初始化structure体variable
struct Student stu = {"张三", 18, 95.5};
// 访问structure体成员
printf("姓名: %s\n", stu.name);
printf("年龄: %d\n", stu.age);
printf("成绩: %.1f\n", stu.score);
// modifystructure体成员
stu.age = 19;
stu.score = 96.5;
printf("\nmodify after :\n");
printf("姓名: %s\n", stu.name);
printf("年龄: %d\n", stu.age);
printf("成绩: %.1f\n", stu.score);
return 0;
}
run结果:
姓名: 张三 年龄: 18 成绩: 95.5 modify after : 姓名: 张三 年龄: 19 成绩: 96.5
4. structure体array
4.1 structure体array 定义 and 初始化
structure体array is 由 many 个相同class型 structure体variable组成 array:
#include <stdio.h>
// 声明structure体
struct Student {
char name[50];
int age;
float score;
};
int main() {
// 定义 and 初始化structure体array
struct Student students[] = {
{"张三", 18, 95.5},
{"李四", 19, 92.0},
{"王五", 18, 88.5}
};
// 计算array long 度
int len = sizeof(students) / sizeof(students[0]);
// 遍历structure体array
printf("学生information:\n");
for (int i = 0; i < len; i++) {
printf("学生%d: 姓名=%s, 年龄=%d, 成绩=%.1f\n",
i+1, students[i].name, students[i].age, students[i].score);
}
return 0;
}
run结果:
学生information: 学生1: 姓名=张三, 年龄=18, 成绩=95.5 学生2: 姓名=李四, 年龄=19, 成绩=92.0 学生3: 姓名=王五, 年龄=18, 成绩=88.5
5. structure体指针
5.1 structure体指针 定义 and 初始化
structure体指针 is 指向structure体variable 指针, 它store is structure体variable memory地址:
#include <stdio.h>
// 声明structure体
struct Student {
char name[50];
int age;
float score;
};
int main() {
// 定义structure体variable
struct Student stu = {"张三", 18, 95.5};
// 定义structure体指针并初始化
struct Student *p = &stu;
return 0;
}
5.2 using箭头运算符(->)访问成员
for 于structure体指针, using箭头运算符(->)来访问其指向 structure体variable 成员:
#include <stdio.h>
// 声明structure体
struct Student {
char name[50];
int age;
float score;
};
int main() {
// 定义structure体variable
struct Student stu = {"张三", 18, 95.5};
// 定义structure体指针并初始化
struct Student *p = &stu;
// using箭头运算符访问成员
printf("姓名: %s\n", p->name);
printf("年龄: %d\n", p->age);
printf("成绩: %.1f\n", p->score);
// modify成员 值
p->age = 19;
p->score = 96.5;
printf("\nmodify after :\n");
printf("姓名: %s\n", p->name);
printf("年龄: %d\n", p->age);
printf("成绩: %.1f\n", p->score);
return 0;
}
run结果:
姓名: 张三 年龄: 18 成绩: 95.5 modify after : 姓名: 张三 年龄: 19 成绩: 96.5
6. 嵌套structure体
6.1 嵌套structure体 声明
structure体可以嵌套 in otherstructure体in, 形成嵌套structure体:
// 声明表示日期 structure体
struct Date {
int year;
int month;
int day;
};
// 声明package含Datestructure体 Studentstructure体
struct Student {
char name[50];
struct Date birthday;
float score;
};
6.2 嵌套structure体 初始化 and 访问
#include <stdio.h>
// 声明表示日期 structure体
struct Date {
int year;
int month;
int day;
};
// 声明package含Datestructure体 Studentstructure体
struct Student {
char name[50];
struct Date birthday;
float score;
};
int main() {
// 初始化嵌套structure体
struct Student stu = {
"张三",
{2000, 1, 1},
95.5
};
// 访问嵌套structure体 成员
printf("姓名: %s\n", stu.name);
printf("生日: %d年%d月%d日\n",
stu.birthday.year, stu.birthday.month, stu.birthday.day);
printf("成绩: %.1f\n", stu.score);
// usingstructure体指针访问
struct Student *p = &stu;
printf("\nusing指针访问:\n");
printf("姓名: %s\n", p->name);
printf("生日: %d年%d月%d日\n",
p->birthday.year, p->birthday.month, p->birthday.day);
printf("成绩: %.1f\n", p->score);
return 0;
}
run结果:
姓名: 张三 生日: 2000年1月1日 成绩: 95.5 using指针访问: 姓名: 张三 生日: 2000年1月1日 成绩: 95.5
7. 联合体
7.1 联合体 concepts
联合体 (Union) is a特殊 user定义dataclass型, 它 and structure体class似, 但 has 一个 important 区别: 联合体 in 所 has 成员共享同一块memory空间. 这意味着联合体 big small etc.于其最 big 成员 big small , 而不 is 所 has 成员 big small 总 and .
联合体 主要用途including:
- 节省memory空间
- implementation不同class型data 共享store
- class型 punning (class型双关)
7.2 联合体 声明 and using
#include <stdio.h>
// 声明联合体
union Data {
int i;
float f;
char c;
};
int main() {
// 定义联合体variable
union Data data;
// 查看联合体 big small
printf("联合体 big small : %d字节\n", sizeof(data));
// using整数成员
data.i = 100;
printf("data.i = %d\n", data.i);
// using浮点数成员 (会覆盖之 before 值)
data.f = 3.14;
printf("data.f = %.2f\n", data.f);
printf("data.i = %d (覆盖 after 值)\n", data.i);
// using字符成员 (会覆盖之 before 值)
data.c = 'A';
printf("data.c = %c\n", data.c);
printf("data.f = %.2f (覆盖 after 值)\n", data.f);
return 0;
}
run结果:
联合体 big small : 4字节 data.i = 100 data.f = 3.14 data.i = 1078523331 (覆盖 after 值) data.c = A data.f = 3.14 (覆盖 after 值)
8. 枚举class型
8.1 枚举class型 声明
枚举 (Enumeration) is auser定义 dataclass型, 它允许定义一组命名 整数常量. 枚举class型 声明语法such as under :
enum 枚举名 {
常量1,
常量2,
// 更 many 常量...
};
// example: 声明一个表示星期 枚举class型
enum Weekday {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
};
默认circumstances under , 枚举常量 值 from 0开始, 依次递增. 也可以显式指定枚举常量 值:
// 显式指定枚举常量 值
enum Weekday {
MONDAY = 1,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
};
8.2 枚举class型 using
#include <stdio.h>
// 声明枚举class型
enum Weekday {
MONDAY = 1,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
};
int main() {
// 定义枚举variable
enum Weekday day;
// 赋值
day = WEDNESDAY;
printf("今天 is 星期%d\n", day);
// using枚举常量
printf("星期一: %d\n", MONDAY);
printf("星期二: %d\n", TUESDAY);
printf("星期三: %d\n", WEDNESDAY);
return 0;
}
run结果:
今天 is 星期3 星期一: 1 星期二: 2 星期三: 3
9. typedef 关键字
9.1 typedef 作用
typedef关键字用于 for 已 has dataclass型creation别名, 使code更简洁, 更易读. 它常用于 for structure体, 联合体 and 枚举class型creation简 short 别名.
9.2 typedef using
// for structure体creation别名
typedef struct {
char name[50];
int age;
float score;
} Student;
// 现 in 可以直接usingStudent而不 is struct Student
Student stu1, stu2;
// for 联合体creation别名
typedef union {
int i;
float f;
char c;
} Data;
// for 枚举class型creation别名
typedef enum {
MONDAY = 1,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
} Weekday;
实践case: 学生informationmanagementsystem
writing一个C程序, usingstructure体 and structure体arrayimplementation一个 simple 学生informationmanagementsystem, including添加学生information, 显示学生information and find学生informationfunctions.
requirementsanalysis
- store many 个学生 information (姓名, 年龄, 成绩)
- 添加学生information
- 显示所 has 学生information
- 根据姓名find学生information
referencecode
#include <stdio.h>
#include <string.h>
// 声明学生structure体
typedef struct {
char name[50];
int age;
float score;
} Student;
// function声明
void addStudent(Student students[], int *count);
void displayStudents(Student students[], int count);
void searchStudent(Student students[], int count);
int main() {
Student students[50]; // 最 many store50个学生
int count = 0; // 当 before 学生数量
int choice;
do {
printf("\n学生informationmanagementsystem\n");
printf("1. 添加学生information\n");
printf("2. 显示所 has 学生information\n");
printf("3. find学生information\n");
printf("4. 退出\n");
printf("请选择operation: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent(students, &count);
break;
case 2:
displayStudents(students, count);
break;
case 3:
searchStudent(students, count);
break;
case 4:
printf("退出system\n");
break;
default:
printf("无效选择, 请重 new 输入\n");
}
} while (choice != 4);
return 0;
}
// 添加学生information
void addStudent(Student students[], int *count) {
if (*count >= 50) {
printf("学生数量已达 on 限\n");
return;
}
printf("请输入学生information:\n");
printf("姓名: ");
scanf("%s", students[*count].name);
printf("年龄: ");
scanf("%d", &students[*count].age);
printf("成绩: ");
scanf("%f", &students[*count].score);
(*count)++;
printf("添加成功\n");
}
// 显示所 has 学生information
void displayStudents(Student students[], int count) {
if (count == 0) {
printf("暂无学生information\n");
return;
}
printf("所 has 学生information:\n");
printf("--------------------------------------------------\n");
printf("姓名\t\t年龄\t成绩\n");
printf("--------------------------------------------------\n");
for (int i = 0; i < count; i++) {
printf("%s\t\t%d\t%.1f\n",
students[i].name, students[i].age, students[i].score);
}
printf("--------------------------------------------------\n");
}
// 根据姓名find学生information
void searchStudent(Student students[], int count) {
if (count == 0) {
printf("暂无学生information\n");
return;
}
char name[50];
printf("请输入要find 学生姓名: ");
scanf("%s", name);
int found = 0;
for (int i = 0; i < count; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("找 to 学生information:\n");
printf("姓名: %s\n", students[i].name);
printf("年龄: %d\n", students[i].age);
printf("成绩: %.1f\n", students[i].score);
found = 1;
break;
}
}
if (!found) {
printf("未找 to 该学生information\n");
}
}
run结果
学生informationmanagementsystem 1. 添加学生information 2. 显示所 has 学生information 3. find学生information 4. 退出 请选择operation: 1 请输入学生information: 姓名: 张三 年龄: 18 成绩: 95.5 添加成功 学生informationmanagementsystem 1. 添加学生information 2. 显示所 has 学生information 3. find学生information 4. 退出 请选择operation: 1 请输入学生information: 姓名: 李四 年龄: 19 成绩: 92.0 添加成功 学生informationmanagementsystem 1. 添加学生information 2. 显示所 has 学生information 3. find学生information 4. 退出 请选择operation: 2 所 has 学生information: -------------------------------------------------- 姓名 年龄 成绩 -------------------------------------------------- 张三 18 95.5 李四 19 92.0 -------------------------------------------------- 学生informationmanagementsystem 1. 添加学生information 2. 显示所 has 学生information 3. find学生information 4. 退出 请选择operation: 3 请输入要find 学生姓名: 张三 找 to 学生information: 姓名: 张三 年龄: 18 成绩: 95.5 学生informationmanagementsystem 1. 添加学生information 2. 显示所 has 学生information 3. find学生information 4. 退出 请选择operation: 4 退出system
互动练习
练习1: writing一个C程序, usingstructure体表示一个矩形, package含 long 度 and 宽度成员, 然 after writingfunction计算矩形 面积 and 周 long .
提示: 定义一个Rectanglestructure体, package含length and width成员, 然 after 定义calculateArea and calculatePerimeterfunction.
练习2: writing一个C程序, usingstructure体 and structure体指针implementation一个链表 basicoperation, including添加node and 显示链表.
提示: 定义一个Nodestructure体, package含data成员 and 指向 under 一个node 指针next, 然 after implementationaddNode and displayListfunction.
练习3: writing一个C程序, using枚举class型表示颜色, 然 after writingfunction根据枚举值输出 for 应 颜色名称.
提示: 定义一个Color枚举, package含RED, GREEN, BLUEetc.常量, 然 after implementationgetColorNamefunction.