Why can't we initialize members inside a struc

2019-01-04 10:22发布

Why can't we initialize members inside a structure ?

example:

struct s {
   int i = 10;
};

6条回答
Viruses.
2楼-- · 2019-01-04 10:42

Edit: The question was originally tagged c++ but the poster said it's regarding c so I re-tagged the question, I'm leaving the answer though...

In C++ a struct is just a class which defaults for public rather than private for members and inheritance.

C++ only allows static const integral members to be initialized inline, other members must be initialized in the constructor, or if the struct is a POD in an initialization list (when declaring the 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?" };
查看更多
▲ chillily
3楼-- · 2019-01-04 10:44

As you said it's just a member not a variable. When you declare a variable the compiler will also provide memory space for those variables where you can put values. In the case a of a struct member the compiler is not giving memory space for it, so you cannot assign values to struct members unless you create a variable of that struct type.

查看更多
再贱就再见
4楼-- · 2019-01-04 10:47

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

struct s { int i=10; };

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

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

As Checkers noted, in C99, you can also use designated initializers (which is a wonderful improvement -- one day, C will catch up with the other features that Fortran 66 had for data initialization, primarily repeating initializers a specifiable number of times). With this simple structure, there is no benefit. If you have a structure with, say, 20 members and only needed to initialize one of them (say because you have a flag that indicates that the rest of the structure is, or is not, initialized), it is more useful:

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

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

查看更多
趁早两清
5楼-- · 2019-01-04 10:58

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

struct s {
   int i = 10;
};

This is an old question, but it ranks high in Google and might as well be clarified.

查看更多
看我几分像从前
6楼-- · 2019-01-04 11:02

We can't initialize because when we declared any structure than actually what we do, just inform compiler about their presence i.e no memory allocated for that and if we initialize member with no memory for that. Normally what happens when we initialize any variable that depends on the place where we declared variable compiler allocate memory for that variable.

int a = 10;
  • if it's auto than in stack memory going to allocate
  • if it's global than in data sections memory going to allocate

So what memory is required to hold that data but in case of structure no memory is there so not possible to initialize it.

查看更多
\"骚年 ilove
7楼-- · 2019-01-04 11:04

If you want to initialize non-static members in struct declaration:

In C++ (not C), structs are almost synonymous to classes and can have members 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 C99 designated initializers, but it's better to use something more portable:

struct s s_instance = {
    i: 10,
};
查看更多
登录 后发表回答