其他分享
首页 > 其他分享> > 定义结构体指针p指向结构体变量

定义结构体指针p指向结构体变量

作者:互联网

#include <stdio.h>
int main()
{
    struct student
    {
        long id;
        char name[20];
        char sex;
        float score;
    };
    struct student stu1={1001,"狂徒张三",'M',89};
    //指针p指向结构体变量
    struct student *p;
    p = &stu1;
    //三种方式调用结构体变量的值
    printf("%d %s %s %f",stu1.id,stu1.name,(stu1.sex)=='M'?"男":"女",stu1.score);
    printf("\n");
    printf("%d\n",(*p).id); //*p一定要带括号,不带括号的意思是*(p.id) .的优先级比*大
    printf("%c\n",p->sex);  // -> 结构体成员运算符
    return 0;
}

标签:stu1,struct,指向,sex,student,printf,结构,id,指针
来源: https://blog.csdn.net/qq_45769071/article/details/122545928