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.)
精彩评论