2

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], 5};
links[0] = link;
Nayuki
  • 17,167
  • 5
  • 51
  • 77
xyz-123
  • 2,145
  • 4
  • 20
  • 26

3 Answers3

5

that's what I was searching for:

links[0] = (Link) {nodes[0], nodes[1], 5};
xyz-123
  • 2,145
  • 4
  • 20
  • 26
  • 1
    Note that this is only valid in C99 and GNU C. Many legacy compilers will not understand this syntax. I mentioned this in the original question this one is a duplicate of. – Juliano Jun 11 '11 at 18:19
1

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

-1

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.)

user541686
  • 197,378
  • 118
  • 507
  • 856