在这些声明下,以下表达式是什么类型,它们是否正确?

时间:2016-06-06 13:15:44

标签: c pointers struct member-variables

struct place 

{

    char name[80+1];
    double latitude;
    double longitude;
};

struct node

{

    struct place city;
    struct node *next;

};

struct node *head;


head
head -> city
head -> next
head -> city -> name
head -> next ->city.name

这些任务总是让我在考试中失分,任何人都可以解释一下吗?它询问所提到的变量的类型,我想像head这样的东西只是指向整个结构node的值的指针?

1 个答案:

答案 0 :(得分:1)

在后面部分的后续片段中,

head -> city -> name

是错误的,因为city不是指针类型。您需要使用点运算符(.)来访问非指针结构变量的成员。就像你在

中使用它一样
head -> next ->city.name

除此之外,从语法上讲,这些片段看起来很好。

只是要补充一点,作为基本的理智,你应该在取消引用之前检查指针的非NULL - 以避免调用undefined behavior