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;
1
  • @Juliano: I found the solution in this topic - see below. Thank you! Commented Jun 11, 2011 at 18:09

3 Answers 3

5

that's what I was searching for:

links[0] = (Link) {nodes[0], nodes[1], 5};
Sign up to request clarification or add additional context in comments.

1 Comment

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

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

1 Comment

the question was actually if it is possible to use the brace syntax in assignments.
-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.)

1 Comment

C99 supports structure literals, which can be used in assignments.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.