嵌入式-C语言:通过结构体指针操作结构体内容
阅读原文时间:2023年07月08日阅读:1

#include
#include
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

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章