简体   繁体   English

在C中初始化struct

[英]initialize struct in c

 typedef struct Node{
        int x;
        int y;
        struct Node* next;
    }Node;

i want create in main "list" in this way: 我想以这种方式在主“列表”中创建:

int main(){
        Node list;
 }

and not in this way: 而不是这样:

int main(){
        Node list = {1,2,NULL};
    }

i want initialize a struct in declaration of struct tryed this way: 我想以这种方式尝试在结构声明中初始化结构:

 typedef struct Node{
        int x;
        int y;
        struct Node* next;
    }Node = {1,2,NULL};

error C2513: 'Node' : no variable declared before '=' 错误C2513:“节点”:在“ =”之前未声明任何变量

need help 需要帮忙

您不能在C中为结构成员提供预定义的值。请使用类似构造函数的函数或类似构造函数的宏。

The typedef (or struct) "statement" defines a type, not an object. typedef(或struct)“声明”定义一种类型,而不是对象。

Types have no value. 类型没有价值。 It only makes sense to speak of values in relation to objects. 谈论与对象有关的价值才有意义。

Objects do not have a default value (other than 0 when they're implicitly initialized). 对象没有默认值(隐式初始化时,其值为0除外)。

So you can't do what you want. 所以你不能做你想做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM