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
的值的指针?
答案 0 :(得分:1)
在后面部分的后续片段中,
head -> city -> name
是错误的,因为city
不是指针类型。您需要使用点运算符(.
)来访问非指针结构变量的成员。就像你在
head -> next ->city.name
除此之外,从语法上讲,这些片段看起来很好。
只是要补充一点,作为基本的理智,你应该在取消引用之前检查指针的非NULL
- 以避免调用undefined behavior。