核心内容摘要
2026冲刺用!AI论文软件 千笔ai写作 VS WPS AI,更贴合本科生需求!
对于STM32F103C8T6集成版进行小升级加入了光敏电阻热敏电阻8个小灯两个按键macial口改成了type-c然后改成了自动下载然后为了省钱又全部改成直插式的了#include stdio.h #include stdlib.h #include string.h //定义结构体数据类型 typedef struct Student { int id; char gender; char name[20]; char *adress;//未分配内存空间之前是野指针 }student; void deepcopy(student *to_stu, student *from_stu) { to_stu-id from_stu-id; to_stu-gender from_stu-gender; //错的 //to_stu-name from_stu-name; strcpy(to_stu-name,from_stu-name); to_stu-adress (char *)malloc(
; memset(to_stu-adress, 0, sizeof(to_stu-adress)); memcpy(to_stu-adress, from_stu-adress, strlen(from_stu-adress)); } int main() { //定义结构体数据变量 // struct Student stu1 {.id 1, .gender M, .name Lisi}; // student stu2 {2, F, xiaohong}; // student *stu stu2; // printf(stu
id %d, stu
gender %c, stu
name %s\n, (stu
-id, stu
gender, stu
name); // printf(stu
id %d, stu
gender %c, stu
name %s\n, (*stu).id, stu-gender, stu-name); struct Student stu1 {1, M}; // printf(please input your message:\n); // scanf(%d %c,stu
id, stu
gender); printf(please input your address:\n); stu
adress (char *)malloc(sizeof(char) *
; scanf(%s,stu
adress); //结构体成员是数组的不能用“”来赋值 //stu
name lisi; strcpy(stu
name, lisi); printf(stu
id %d, stu
gender %c, stu
name %s\n, (stu
-id, stu
gender, stu
name); printf(address:%s\n,stu
adress); //结构体变量之间的赋值,浅拷贝问题 student stu2; // stu1; deepcopy(stu2, stu
; free(stu
adress); stu
adress NULL; printf(stu
id %d, stu
gender %c, stu
name %s\n, stu
id, stu
gender, stu
name); printf(address:%s\n,stu
adress); free(stu
adress); stu
adress NULL; return 0; }#include stdio.h struct Student { int id; char gender; char name[20]; }; //结构体字节对齐是什么 //结构体的内存对齐 //要在最大的数据类型上根据你代码中的注释我来详细讲解结构体对齐规则 // 结构体对齐规则 // 核心规则 // 对齐值结构体中最大成员的类型大小如int为4double为8 // 成员偏移每个成员的偏移量必须是其类型大小的整数倍 // 结构体总大小必须是最大成员大小的整数倍 // 以你的struct Student为例 // struct Student { // int id; // 4字节偏移0 ✓ // char gender; // 1字节偏移4 ✓ // char name[20]; // 20字节偏移5 (5%41不满足) // }; // 问题name偏移量为5但应该是4的倍数因为最大成员是int4字节 // 内存布局 // 偏移
: id (4字节) // 偏移 4: gender (1字节) // 偏移 5: 填充3字节 (对齐到偏移
// 偏移
: name (20字节) // 总大小28字节 // 实际输出28 // 优化版本按类型大小从大到小排列 // struct Student { // int id; // 偏移04字节 // char name[20]; // 偏移420字节 // char gender; // 偏移241字节 // char padding[3]; // 填充3字节总大小28字节 // }; // 注意这个例子中两种写法大小相同但通常按大小排列能减少浪费。
int main() { printf(%d\n,sizeof(struct Student)); return 0; }、#include stdio.h #include stdlib.h #include string.h #include time.h int main() { //共用体union union Data { int i; float f; char str[20]; } data; //共用体的特点所有成员共享同一块内存空间只能同时存储一个成员的数据 return 0; }#include stdio.h int main() { //关键字volatile的作用 //