C语言精讲-09
1.结构体(struct)
1.1什么是结构体?
我们在现实⽣活中,在对具体的对象进⾏描述的时候,发现对象是⽐较复杂的。⼀般是由不同的类型组合在⼀起的。例如:我们描述⼀个⼈的时候,习惯性会描述他的姓名,年龄,分数等。这些不同类型的数据是互相联系组成了⼀个有机的整体。此时,就要⽤到⼀种新的构造类型数据——结构体(struct)。
1.2声明结构体
1.3 声明结构体变量并调用成员
声明结构体变量格式1
#include <stdio.h>
#include <string.h>
// 定义 Student 结构体
struct Student {
int id;
char name[50];
char gender;
char address[100];
};
int main() {
struct Student stu1; // 声明结构体变量
// 调用结构体成员
stu1.id = 1001;
// stu1.name = "Tom"; // 报错,不能直接通过赋值运算符来给字符数组赋值
strcpy(stu1.name, "Tony");
stu1.gender = 'M';
strcpy(stu1.address, "中国");
printf("id = %d,name = %s,gender = %c,address = %s\n",
stu1.id, stu1.name, stu1.gender, stu1.address);
return 0;
}
声明结构体变量格式2:
#include <stdio.h>
//声明结构体
struct Car {
char* name;
double price;
int speed;
};
int main() {
//声明结构体变量
struct Car audi = {"audi A6L", 460000.99, 175};
// 输出结构体变量的信息
printf("Car Name: %s\n", audi.name);
printf("Car Price: %.2f\n", audi.price);
printf("Car Speed: %d\n", audi.speed);
return 0;
}
声明结构体变量格式3:
#include <stdio.h>
// 定义一个结构体
struct Person {
char name[50];
int age;
float height;
char gender;
};
int main() {
// 使用指定初始化器初始化结构体变量
struct Person p = {.name = "Alice", .age = 25, .height = 1.65, .gender = 'F'};
// 输出结构体成员的值
printf("Name: %s\n", p.name);
printf("Age: %d\n", p.age);
printf("Height: %.2f\n", p.height);
printf("Gender: %c\n", p.gender);
return 0;
}
声明结构体变量格式4
#include <stdio.h>
// 定义 Circle 结构体并声明变量 c1
struct Circle {
int id;
double radius;
} c1;
int main() {
// 给结构体变量 c1 的成员赋值
c1.id = 1;
c1.radius = 5.0;
// 输出结构体变量 c1 的成员信息
printf("Circle ID: %d\n", c1.id);
printf("Circle Radius: %.2f\n", c1.radius);
return 0;
}
声明结构体变量格式5(常用)
#include <stdio.h>
//声明结构体
typedef struct cell_phone {
long long phone_no; // 电话号码,使用 long long 避免溢出
double minutes_of_charge; // 每分钟费用
} Phone;
int main() {
//声明结构体变量
Phone p = {13012341234LL, 0.5}; // 使用 LL 后缀表示 long long 类型常量
// 输出结构体变量信息
printf("Phone Number: %lld\n", p.phone_no);
printf("Minutes of Charge: %.2f\n", p.minutes_of_charge);
return 0;
}
1.4结构体嵌套
结构体的成员也是变量,那么成员可以是 基本数据类型 ,也可以是 数组 、 指针 、结构体 等类型 。如果结构体的成员是另一个结构体,这就构成了结构体嵌套。
#include <stdio.h>
// 定义 Date 结构体
struct Date {
int year;
int month;
int day;
};
// 定义 Student 结构体,嵌套 Date 结构体
struct Student {
char name[50];
int id;
struct Date birthDate;
};
int main() {
// 初始化 Student 结构体变量
struct Student stu = {
"Alice",
1001,
{2005, 3, 15}
};
// 输出学生信息
printf("Student Name: %s\n", stu.name);
printf("Student ID: %d\n", stu.id);
printf("Birth Date: %d-%d-%d\n", stu.birthDate.year, stu.birthDate.month, stu.birthDate.day);
return 0;
}
2.结构体数组
结构体数组是一种由多个相同结构体类型元素组成的数组。在实际应用中,当需要处理多个具有相同属性集合的对象时,使用结构体数组就非常方便。比如,在管理学生信息时,每个学生都有姓名、年龄、成绩等属性,就可以定义一个学生结构体,然后用结构体数组来存储多个学生的信息。
#include <stdio.h>
#include <string.h>
// 定义 Book 结构体
struct Book {
char title[100];
char author[50];
int year;
};
int main() {
// 定义并初始化结构体数组
struct Book books[3] = {
{"C Programming Language", "Brian W. Kernighan", 1978},
{"Effective C++", "Scott Meyers", 1991},
{"The C++ Programming Language", "Bjarne Stroustrup", 1985}
};
// 输出每本书的信息
for (int i = 0; i < 3; i++) {
printf("Book %d:\n", i + 1);
printf("Title: %s\n", books[i].title);
printf("Author: %s\n", books[i].author);
printf("Year: %d\n", books[i].year);
printf("\n");
}
return 0;
}
3.结构体指针
结构体指针是指向结构体变量的指针,它存储的是结构体变量的内存地址。借助结构体指针,能够间接访问和操作结构体的成员。相较于直接使用结构体变量,结构体指针在某些场景下具有独特的优势,比如在函数间传递结构体数据时,传递结构体指针可以避免复制整个结构体,从而提高效率;同时,在动态内存分配中,常常会使用结构体指针来管理动态分配的结构体对象。
#include <stdio.h>
#include <string.h>
// 定义 Person 结构体
struct Person {
char name[50];
int age;
char gender;
};
int main() {
// 定义一个 Person 结构体变量
struct Person person = {"Alice", 25, 'F'};
// 定义一个指向 Person 结构体的指针,并指向 person 变量
struct Person *ptr = &person;
// 使用结构体指针访问结构体成员
printf("Name: %s\n", ptr->name);
printf("Age: %d\n", ptr->age);
printf("Gender: %c\n", ptr->gender);
// 使用结构体指针修改结构体成员
strcpy(ptr->name, "Bob");
ptr->age = 30;
ptr->gender = 'M';
// 再次输出修改后的结构体成员
printf("\nAfter modification:\n");
printf("Name: %s\n", ptr->name);
printf("Age: %d\n", ptr->age);
printf("Gender: %c\n", ptr->gender);
return 0;
}
4.结构体占用内存大小
内存对齐是编译器为了提高内存访问效率而采用的一种策略。它会让结构体的每个成员按照特定的字节边界进行存储,使得每个成员的起始地址是其自身大小的整数倍。同时,结构体的总大小也会是其最大成员大小的整数倍。
以4个字节对齐为案例
#include <stdio.h>
// 示例结构体 1
struct Example1 {
char c; // 1 字节
int i; // 4 字节
short s; // 2 字节
};
// 示例结构体 2
struct Example2 {
int i; // 4 字节
short s; // 2 字节
char c; // 1 字节
};
int main() {
// 输出结构体大小
printf("Size of struct Example1: %zu bytes\n", sizeof(struct Example1));
printf("Size of struct Example2: %zu bytes\n", sizeof(struct Example2));
return 0;
}
###注意:仅当个人笔记,不喜勿喷,有错误欢迎在评论区指出来。