【问题标题】:error: expected expression before '{' token|错误:“{”标记之前的预期表达式|
【发布时间】:2015-03-28 23:03:17
【问题描述】:
  struct node{
        int data;
        struct node *next;
    };
main(){
    struct node a,b,c,d;
    struct node *s=&a;

    a={10,&b};
    b={10,&c};
    c={10,&d};
    d={10,NULL};


    do{
        printf("%d %d",(*s).data,(*s).next);

        s=*s.next;
    }while(*s.next!=NULL);

}

在 a={10,&b};Expression syntex error 处显示错误。请帮助.. 提前谢谢你

【问题讨论】:

标签: c struct linked-list


【解决方案1】:

立即初始化变量:

struct node a={10,NULL};

然后分配地址:

a.next = &b;

或者使用复合文字:

a=( struct node ){10,&b};  //must be using at least C99

【讨论】:

  • 为什么我们在 a=(struct node){10,&b} 中初始化时进行类型转换。请详细解释...thankzzz
  • @RohitGoel 这是一个赋值,a 已经初始化。那是一个复合字面量,也是使用它的选定语法。
  • @RohitGoel 您对 cast 的查询在我的回答中提供的链接中得到了很好的解释。请检查一下。
【解决方案2】:

在定义时允许使用仅括号括起来的列表初始化结构变量。使用

struct node a={10,&b};

否则,您必须使用compound literal [c99 及以上]

a=(struct node){10,&b};

【讨论】:

    猜你喜欢
    • 2014-02-22
    • 2021-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 2017-09-24
    • 2012-03-09
    相关资源
    最近更新 更多