当前位置: 首页 > news >正文

寒假学习(8)(c语言8+模数电8)

对于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(100); 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("stu1.id = %d, stu1.gender = %c, stu1.name = %s\n", (&stu1)->id, stu1.gender, stu1.name); // printf("stu2.id = %d, stu2.gender = %c, stu2.name = %s\n", (*stu).id, stu->gender, stu->name); struct Student stu1 = {1, 'M'}; // printf("please input your message:\n"); // scanf("%d %c",&stu1.id, &stu1.gender); printf("please input your address:\n"); stu1.adress = (char *)malloc(sizeof(char) * 100); scanf("%s",stu1.adress); //结构体成员是数组的不能用“=”来赋值 //stu1.name = "lisi"; strcpy(stu1.name, "lisi"); printf("stu1.id = %d, stu1.gender = %c, stu1.name = %s\n", (&stu1)->id, stu1.gender, stu1.name); printf("address:%s\n",stu1.adress); //结构体变量之间的赋值,浅拷贝问题 student stu2; //= stu1; deepcopy(&stu2, &stu1); free(stu1.adress); stu1.adress = NULL; printf("stu2.id = %d, stu2.gender = %c, stu2.name = %s\n", stu2.id, stu2.gender, stu2.name); printf("address:%s\n",stu2.adress); free(stu2.adress); stu2.adress = NULL; return 0; }
#include <stdio.h> struct Student { int id; char gender; char name[20]; }; //结构体字节对齐是什么 //结构体的内存对齐 //要在最大的数据类型上根据你代码中的注释,我来详细讲解结构体对齐规则: // 结构体对齐规则 // 核心规则: // 对齐值:结构体中最大成员的类型大小(如int为4,double为8) // 成员偏移:每个成员的偏移量必须是其类型大小的整数倍 // 结构体总大小:必须是最大成员大小的整数倍 // 以你的struct Student为例: // struct Student { // int id; // 4字节,偏移0 ✓ // char gender; // 1字节,偏移4 ✓ // char name[20]; // 20字节,偏移5 (5%4=1,不满足) // }; // 问题:name偏移量为5,但应该是4的倍数(因为最大成员是int,4字节) // 内存布局: // 偏移 0-3: id (4字节) // 偏移 4: gender (1字节) // 偏移 5: 填充3字节 (对齐到偏移8) // 偏移 8-27: name (20字节) // 总大小:28字节 // 实际输出:28 // 优化版本(按类型大小从大到小排列): // struct Student { // int id; // 偏移0,4字节 // char name[20]; // 偏移4,20字节 // char gender; // 偏移24,1字节 // 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的作用 //1.防止编译器优化 //2.防止编译器将变量保存到寄存器中 volatile int i = 0 ; int *p = &i; //取地址为什么会报错: //int *p = &i; //不能去取i的地址,因为i是volatile的,编译器会认为i的值随时可能发生变化,所以编译器会禁止对i的取地址操作 while(1) { printf("i = %d\n",i); } return 0; }
#include <stdio.h> void func(void) { static int i = 0; i++; printf("i = %d\n",i); } int main() { //关键字static的作用 //1.修饰局部变量:静态局部变量,生命周期长,作用域局部 //2.修饰全局变量:静态全局变量,生命周期长,作用域全局 //3.修饰函数:静态函数,只能在定义它的文件中使用 //4.修饰类:静态类,只能在定义它的文件中使用 func(); func(); func(); return 0; }
http://www.jsqmd.com/news/314311/

相关文章:

  • 什么是无需编程的全栈开发平台?一文讲清原理与价值
  • 详细介绍:JavaEE进阶——SpringMVC响应处理详解
  • 相当完美的新一代移动处理器!英特尔酷睿Ultra X9 388H实测
  • mybatis中collection标签与association标签的区别与应用场景
  • 搭配单通道高延迟内存照样是最强游戏处理器!锐龙7 9850X3D首发评测
  • 2025年CRM系统选型手册:主流厂商能力横向对比及深度解析
  • SpringMVC
  • Naabu 使用手册
  • 声振温监测技术:设备故障的“隐形哨兵”,预警的核心底气
  • 设计行业资讯精准推送工具,输入关注行业关键词,自动筛选优质资讯,过滤冗余信息,按每日/每周推送,帮职场人及时掌握行业动态。
  • 2026必备!9个AI论文写作软件,MBA论文写作神器推荐!
  • AI 赋能大模型的下一个“风口”在哪?
  • 多模态大模型中Attention机制暗藏「骗局」,需用一个公式修正丨上大×南开
  • Python RPA从零到实战:一份为期100天的系统培训大纲
  • 2026年CTO最想招的不是程序员,而是“懂业务的测试者”
  • 2026年最值钱的软技能TOP3:软件测试从业者的转型指南
  • 安卓手机/平板/TV版 Rotation强制横屏显示设备!免ROOT可用!再推荐突破手机限制的3款神器
  • 1-28午夜盘思
  • 使用 JYPPX.DeploySharp 高效部署 PaddleOCR,解锁多种高性能 OCR 文字识别方案
  • memset和memcpy的区别
  • 如何把post train做好,后训练方法论
  • LLM已死?Agentic Reasoning:重塑LLM智能体思维
  • 云拒科技推出Yunjue Agent:能够从零开始自我进化的助手系统
  • 基于Java+SSM的智能停车场管理系统:集成车牌识别(SVM)与数据可视化(可用作毕设参考)
  • Flutter艺术探索-Flutter依赖注入:get_it与provider组合使用
  • 设计客户需求整理工具,录入客户沟通内容,自动提取核心需求,异议点及诉点,生成需求清单,标注优先级,帮销售/运营精准对接客户需求。
  • Flutter艺术探索-设计模式在Flutter中的应用:单例、工厂、观察者
  • 大数据时代 RabbitMQ 对数据安全的防护
  • 基于SpringBoot的私房菜定制上门服务系统毕设源码
  • 科视Christie在ISE 2026诠释热忱与机遇的交融魅力