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,911
  • 6
  • 53
  • 80
xyz-123
  • 2,227
  • 4
  • 21
  • 27

3 Answers3

5

that's what I was searching for:

links[0] = (Link) {nodes[0], nodes[1], 5};
xyz-123
  • 2,227
  • 4
  • 21
  • 27
  • 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
  • 205,094
  • 128
  • 528
  • 886