Take the 2-minute tour ×

I could do struct initialization with code:

struct struct_type_id struct_name_id = { value1, value2, value3 };

but could not with:

struct struct_type_id struct_name_id;
struct_name_id = { value1, value2, value3 };

why I could do it with the former,but could not with the latter with gcc,g++,vc2008,vc6?In other words,why the c/c++ programming language do not support this syntax?


share|improve this question
Where can I learn more about this C/C++ programming language? Does it have a Web page? –  n.m. May 28 at 2:33

5 Answers 5

up vote 19 down vote accepted

The first statement creates a variable initialized to the given values, i.e., these values are built in memory and stored directly in the program executable in that variable address (for globals) or ready for memory copy (for stack variables).

The second statement of the second block is very different. Although it looks similar, it is an assign expression. It means that the RHS of the equals operator is an expression that is evaluated (independently of what is in the LHS of =), and then passed to the = operator. Without proper context, {...} doesn't have any meaning.

In C99, you can do this:

struct_name_id = (struct struct_type_id){ value1, value2, value3 };

Now the RHS of the equals operator is a valid expression, since there is proper context for the compiler to know what is in {...}.

In C++11, the syntax is:

struct_name_id = struct_type_id{ value1, value2, value3 };
share|improve this answer
@Juliano:C89 also support this type cast syntax acordding to gcc. –  Jichao Nov 10 '09 at 3:29
@jcyang: GCC might permit that syntax, but it's not C89/C90 (it's a GCC extension). For example, MSVC, Digital Mars and Comeau do not (unless you turn on C99 support in Comeau) support that syntax. –  Michael Burr Nov 10 '09 at 5:41
@michael:but i used the switch --std=c89 –  Jichao Nov 10 '09 at 13:53
@jcyang: try with -Wall -pedantic --std=c89 –  Juliano Nov 10 '09 at 14:46

You just need to cast the values as such:

share|improve this answer

I don't know why C didn't originally support some kind of syntax to 'reinitialize' a struct using something like the initializer list - there are definitely times when I would have found it handy. As Juliano mentioned, C99 (and C++0x) has fixed it to a certain degree, but I often have to stick with C90. When I want to do something like that, I'll sometimes do the following:

struct foo const init_foo = { 1, 2, 3};
struct foo myFoo;

// ....

myFoo = init_foo;  // reinitialize myFoo
share|improve this answer

I faced a similar problem, and the solution to that was that I was trying to initialized the struct outside the function(not using the initializer syntax, but with the obj.member = VALUE; notation). It is a related problem, so posting here, hoping someone with the same question lands up here.

share|improve this answer

Will this work for you ?

typedef struct name_id {int value1; int value2; int value3;} NAME_ID;
name_id mynameid = {0,1,2};
share|improve this answer
@FractalSpace:It could not work.It just define one alias to the struct name_id but do not change any other things. –  Jichao Nov 10 '09 at 3:32

Your Answer


