14.5 结构和其他数据形式:指针和结构

【结构声明】

//结构数组定义
struct names {
    char first[LEN];
    char last[LEN];
};

struct guy {
    struct names handle;
    char food[LEN];
    char job[LEN];
    float income;
};

//结构数组初始化
struct guy fellow[2] = {
        {{"enent","villard"},
        "grilled salmon",
        "personality coach",
        6812.0
        },
        {{"wnter","swillbelly"},
        "tripe",
        "tabloid editor",
        4300.2
        }
    };

声明结构指针

struct guy * him;

关键字struct、结构标记guy、指针标志 * 、其后是指针名。

如果barney是一个guy类型结构变量,则

him = & barney;

若fellow是一个结构数组,那么fellow[0]是一个结构,him指向可以写作

him = &fellow[0];

[指针访问结构成员]
如果him是指向guy类型结构的barney指针,则

//假设him = &barney
barney.income == (*him).imcome == him -> imcome

同样

//him == &fellow[0]
fellow[0].income == him -> income == (*him).income

推荐阅读更多精彩内容