嵌入式-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

posted @ 2022-11-03 22:40  WellMandala  阅读(37)  评论(0编辑  收藏  举报