以下笔记参考我自己学习micro_frank的C语言课程 快来一起学习吧
结构体的定义、创建和具体使用涉及知识点
- 结构体的基本定义和使用方式
- 通过.去访问成员
- 可以通过指针去访问
#include <inttypes.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
//这是关于结构体的全部入门知识点讲解
//1.首先是有关结构体的基本定义和使用方式(结构体一般放全局,这样可以在整个代码都可以用了)
//结构体Structures它类似于我们之前学习的int整型、float浮点型...
//struct给了我们想要定义任意数据类型的权利
//e.g.我们想要定义一个表示日期的结构体Date
//其中整个结构体包含了3个int类型的成员
//创建结构体
struct Date {
int Day;
int Month;
int Year;
//....
};
//可以类比想到定义了一个today变量,变量类型是struct
struct Date tomorrow = { 1,2,2024 };
//还可以这样子写,我们就不想写struct Date,这个struct
//比如使用typedef
typedef struct Person {
char name[10];
int age;
float height;
//...
}PP;
PP Capoo = { "capoo",10,12 };
int main(void) {
//初始化结构体变量
struct Date today = { 12,1,2024 };
//通过.去访问成员
printf("Today's date is : %d-%d-%d\n",today.Year,today.Month,today.Day);
//还可以通过指针去访问
struct Date* date_ptr = &today;
printf("Today's date is :%d-%d-%d\n",date_ptr->Year,date_ptr->Month,date_ptr->Day);
return 0;
}
小小应用一下写一个学生成绩管理系统
涉及知识点:
- 匿名结构体
- 结构体成员的函数,使用指针实现
- 局部变量
#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#include <stdbool.h>
//在接下来的写的是利用结构体(Structure)写一个学生成绩管理系统
//如果后面写了Student,那么struct后面可以将Student省略,就是匿名结构体
typedef struct Student {
char name[50];
int id;
float score;
}Student;
//打印结构体成员的函数,传参要把类型带上,不要忘记了!!
void print_stu(Student stu);
//更新学生成绩的函数,注意要传的是地址,不然局部变量会释放
void update_score_by_value(Student* stu, float new_score);
int main(void) {
Student stu = { "Capoo",123,99 };
puts("Before update:");
//打印结构体要经常用到,所以定义一个函数
print_stu(stu);
//更新值,注意用取地址
update_score_by_value(&stu,100);
puts("\nUpdate score by value:");
print_stu(stu);
return 0;
}
void print_stu(Student stu) {
printf("Student Name: %s\n", stu.name);
printf("Student ID: %d\n", stu.id);
printf("Student Score: %.2f\n", stu.score);
}
void update_score_by_value(Student* stu, float new_score) {
stu->score = new_score;
}
值语义初始化结构体变量
- 创建一个结构体函数
- 这么做有什么讲究呢?安全(因为不是在main里面定义的)、所有用户进入程序可以设置初始化相同的坐标点Point
#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#include <stdbool.h>
//通过值返回结构体
typedef struct {
int x;
int y;
}Point;
//创建一个结构体函数
//这么做有什么讲究呢?
//安全(因为不是在main里面定义的)、所有用户进入程序可以设置初始化相同的坐标点Point
Point get_point(void);
int main(void) {
Point my_point = get_point();
printf("Point :(%d,%d)\n",my_point.x,my_point.y);
return 0;
}
//函数调用完就销毁了,所以很夸张,没有办法被黑客攻击初始值
Point get_point(void) {
//值语义(values semastics)
Point p = { 10,20 };
return p;//return一个结构体的副本
}
结构体数组
#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#include <stdbool.h>
//通过值返回结构体
typedef struct {
int x;
int y;
}Point;
int main(void) {
//定义一个包含2个结构体的数组
Point points[2] = {
{1,2},{3,4}
};
//输出数组(基本操作了,一定要学会哦)
for (size_t i = 0; i < 2; i++) {
printf("Point %d:(%d,%d)\n", i, points[i].x, points[i].y);
}
return 0;
}
嵌套结构体
#include <stdio.h>
typedef struct {
char street[50];
char city[50];
char country[50];
}Address;
typedef struct {
char name[50];
int age;
Address address;//嵌套结构体
}Person;
int main(void) {
Person frank = {
"Capoo",
100,
{"Cat","asxxx","China"}
};
//访问
printf("Name: %s\n",frank.name);
printf("Address :%s,%s,%s\n",frank.address.street, frank.address.city, frank.address.country);
Person* ptr = &frank;
printf("Name:%s\n",ptr->name);
printf("Address:%s,%s,%s,%s\n",ptr->address .street,ptr->address.city,ptr->address.country);
return 0;
}