开发者

Can C struct assignment use brace syntax? [duplicate]

开发者 https://www.devze.com 2023-03-12 10:27 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Struct initialization of the C/C++ programming language?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Struct initialization of the C/C++ programming language?

I'm re-learning C and asking myself if something like this is possible:

typedef struct Link {
    struct Node a;
    struct Node b;
    float weight;
    } Link;

Link links[LINK_NUMBER];
links[0] = {nodes[0], nodes[1], 5};

instead of:

Link link0 = {nodes[0], nodes[1]开发者_如何学C, 5};
links[0] = link;


that's what I was searching for:

links[0] = (Link) {nodes[0], nodes[1], 5};


Are you asking if structs can be assigned? If so, the answer is yes.


That doesn't work because a Link can't contain another Link.

However, it can contain a pointer to another Link (a Link*).

Regarding the assignment: You can only use the brace syntax when initializing a value, not when setting a value. (I believe this changes in C++0x/C++11, though.)

0

精彩评论

暂无评论...
验证码 换一张
取 消