在这些声明中,以下表达式是什么类型,它们是否正确?
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
值的指针?
在后面部分的后面片段中,
head -> city -> name
是错的,因为, city
不是指针类型。 您需要使用点运算符( .
)来访问非指针结构变量的成员。 就像你使用它的方式一样
head -> next ->city.name
除此之外,在语法上,片段看起来很好。
只是要补充一点,作为一个基本的健全性,您应该在取消引用之前检查指针的非NULL
值,以避免调用未定义的行为 。