• Advertisement
Sign in to follow this  

Initializing structures

This topic is 3830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, it's me again. This time with a problem initializing structures. I gues this code fragment says more than thousend words: Case 1: struct abc{int a,b;} xyz = {1,2}; Everything works great! Case 2: struct abc{int a,b;} xyz; xyz = {1,2}; I get the error: "syntax error: '{'" What the ****? Is this some kind of joke?

Share this post


Link to post
Share on other sites
Advertisement
Initialization only occurs during definition, and you can only use compound literals within an initialization.

struct abc{int a,b;} xyz;
abc temp = {1,2}; // Initialize temp
xyz = temp; // Assign to xyz

Share this post


Link to post
Share on other sites
Assuming this is C++ or C pre-C99, that's illegal. On the off chance you are programming in C99, you can use compound literals to do something like:

xyz = (struct abc){1,2};

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement