zoukankan      html  css  js  c++  java
  • (C语言)结构体成员的引用->(箭头)和 .(点)

    关于结构体成员的引用有这样的规律:

    箭头(->):左边必须为指针;
    点号(.):左边必须为实体。

    那么如果一个结构体指针引用一个成员,这个成员又是一个结构体(并且是一个实体),那么如果要引用这个成员的成员要怎么办呢?

    经过实验发现,依然遵循上面的规则即:箭头左边必须是指针,实体一定要用点号引用。for example C->student.age

    eg.

    一开始都用箭头即c->s1->age

    #include "stdio.h"
    int main()
    {
        struct student{
            int age;
            int class_;
        };
        struct class2{
            struct student s1;
        };
        struct class2 *c;
        struct class2 cc={
        .s1={
            .age=9,
            .class_=2
        }
        };
        c=&cc;
        printf("%d",c->s1->age);
        return 0;
    }

    出现错误如下:

    如果把代码改为c->s1.age,代码如下:

    #include "stdio.h"
    int main()
    {
        struct student{
            int age;
            int class_;
        };
        struct class2{
            struct student s1;
        };
        struct class2 *c;
        struct class2 cc={
        .s1={
            .age=9,
            .class_=2
        }
        };
        c=&cc;
        printf("%d",c->s1.age);
        return 0;
    }

    则编译通过得到运行结果:

  • 相关阅读:
    9.19题解
    9.18题解
    改码风
    找到了几个好的网站
    题目链接
    二分上机训练题解
    二分例题简单说明
    贪心上机训练题解
    贪心算法例题简单说明
    Johnson法则证明
  • 原文地址:https://www.cnblogs.com/winifred-tang94/p/5843440.html
Copyright © 2011-2022 走看看