Why can't we initialize the elements inside the structure?

Why can't we initialize the elements inside the structure?

Example:

struct s { int i = 10; }; 
+43
c initialization struct member
Oct 22 '08 at 10:01
source share
6 answers

If you want to initialize non - static elements in a struct declare :

In C ++ (not C), structs almost synonymous with classes and can have elements initialized in the constructor.

 struct s { int i; s(): i(10) { } }; 

If you want to initialize an instance :

In C or C ++:

 struct s { int i; }; ... struct s s_instance = { 10 }; 

C99 also has a feature called designated initializers:

 struct s { int i; }; ... struct s s_instance = { .i = 10, }; 

There is also a GNU C extension, which is very similar to the initializers assigned by C99, but it is better to use something more portable:

 struct s s_instance = { i: 10, }; 
+34
Oct 22 '08 at 10:03
source share

The direct answer is that the definition of the structure declares a type, not a variable that can be initialized. Your example:

 struct s { int i=10; }; 

This does not declare any variable - it defines the type. To declare a variable, you must add a name between } and ; and then you will initialize it later:

 struct s { int i; } t = { 10 }; 

As Checkers noted, on C99 you can also use designated initializers (which is a great improvement - one fine day C will catch up with the other functions that Fortran 66 provided to initialize the data, primarily repeating the initializers a certain number of times). There is no use with this simple structure. If you have a structure with, say, 20 members, and you only need to initialize one of them (say, because you have a flag indicating that the rest of the structure is initialized or not), this is more useful:

 struct s { int i; } t = { .i = 10 }; 

This notation can also be used to initialize unions to choose which union element is initialized.

+31
Oct 22 '08 at 15:14
source share

Edit: The question was originally tagged c++ , but the poster said that it refers to c , so I again marked the question, I leave the answer, though ...

In C ++, a struct is just a class , which uses public by default rather than private for members and inheritance.

C ++ allows initialization of static const integral elements, other members must be initialized in the constructor or if struct is a POD in the initialization list (when declaring a variable).

 struct bad { static int answer = 42; // Error! not const const char* question = "what is life?"; // Error! not const or integral }; struct good { static const int answer = 42; // OK const char* question; good() : question("what is life?") // initialization list { } }; struct pod { // plain old data int answer; const char* question; }; pod p = { 42, "what is life?" }; 
+7
Oct 22 '08 at 10:05
source share

Note that in C ++ 11, the following declaration is now allowed:

 struct s { int i = 10; }; 

This is an old question, but it is high on Google and can also be clarified.

+6
May 31 '16 at 16:12
source share

We cannot initialize, because when we declare some structure than what we actually do, just inform the compiler of their presence. If memory allocation is not allocated for this, and if we initialize an element without memory for this. Usually what happens when we initialize any variable that depends on where we declared the variable to the compiler, allocate memory for that variable.

 int a = 10; 
  • if it is auto than on the stack, it will stand out
  • if it is global than in the area of ​​data partitions, stands out

So, what kind of memory is required to store this data, but in the case of a structure there is no memory, so it cannot be initialized.

+2
Mar 14 '16 at 18:14
source share

As you said, this is just a member, not a variable. When you declare a variable, the compiler also provides memory space for those variables where you can put values. In case of a structure element, the compiler does not allocate memory space for it, so you cannot assign values ​​to structure members unless you create a variable of this structure type.

0
May 31 '16 at 16:26
source share



All Articles