嵌入式-C语言:通过结构体指针操作结构体内容
#include<stdio.h> #include<string.h> struct Student { char name[32]; int age; int height; int weight; }; int main() { struct Student stu1={"hhh",12,45,45}; struct Student * stu1P=&stu1; //通过指针访问结构体 printf("name=%s\n",stu1P->name); printf("age=%d\n",stu1P->age); printf("height=%d\n",stu1P->height); printf("weight=%d\n",stu1P->weight); //通过结构体指针修改结构体 stpcpy(stu1P->name,"ttt"); //stu1P->name="ttt";//错误,字符串不能直接这么赋值,要使用strcpy stu1P->age=180; stu1P->height=280; stu1P->weight=450; printf("name=%s\n",stu1P->name); printf("age=%d\n",stu1P->age); printf("height=%d\n",stu1P->height); printf("weight=%d\n",stu1P->weight); return 0; }
输出结果:
name=hhh
age=12
height=45
weight=45
name=ttt
age=180
height=280
weight=450
4556
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端