【问题标题】:Under these declarations, what types are the following expressions and are they correct? [closed]在这些声明下,以下表达式是什么类型,它们是否正确? [关闭]
【发布时间】:2016-06-06 13:15:44
【问题描述】:
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 的值的指针?

【问题讨论】:

  • 是的,基本上你是对的。您有具体问题吗?
  • 代码下面这5行是否有错误的表达,为什么会出错?
  • head -> city -> name 似乎不对。
  • 我投票决定将此问题作为题外话结束,因为 SO 的目的不是解释 c 的基本原则,而是帮助解决代码的特定问题。

标签: c pointers struct member-variables


【解决方案1】:

在后面的sn-p中,

head -> city -> name

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

head -> next ->city.name

除此之外,从语法上看,sn-ps 看起来不错。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 2020-08-07
    • 2012-11-17
    • 2015-03-18
    • 1970-01-01
    相关资源
    最近更新 更多