为什么我们不能初始化结构体中的成员?

发布于 2024-07-07 21:27:27 字数 94 浏览 7 评论 0原文

为什么我们不能初始化结构体中的成员?

例子:

struct s {
   int i = 10;
};

Why can't we initialize members inside a structure ?

example:

struct s {
   int i = 10;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

笛声青案梦长安 2024-07-14 21:27:27

如果您想在struct声明中初始化非静态成员:

在C++(不是C)中,struct是几乎与类同义,并且可以在构造函数中初始化成员。

struct s {
    int i;

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

如果你想初始化一个实例

在C或C++中:

struct s {
    int i;
};

...

struct s s_instance = { 10 };

C99还有一个称为指定初始化器的功能:

struct s {
    int i;
};

...

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

还有一个GNU C扩展,它与C99指定初始化器非常相似,但最好使用更便携的东西:

struct s s_instance = {
    i: 10,
};

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,
};
故事和酒 2024-07-14 21:27:27

直接的答案是因为结构体定义声明了一个类型,而不是一个可以初始化的变量。 你的例子是:

struct s { int i=10; };

这没有声明任何变量 - 它定义了一个类型。 要声明变量,您需要在 }; 之间添加一个名称,然后再初始化它:

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

正如 Checkers 所指出的,在 C99 中,您还可以使用指定的初始化器(这是一个很棒的改进——有一天,C 将赶上 Fortran 66 数据初始化的其他功能,主要是重复初始化器指定的次数)。 这种简单的结构没有任何好处。 如果您有一个包含 20 个成员的结构,并且只需要初始化其中一个成员(例如,因为您有一个标志表明该结构的其余部分已初始化或未初始化),那么它会更有用:

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

此表示法也可用于初始化联合,以选择初始化联合的哪个元素。

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.

好听的两个字的网名 2024-07-14 21:27:27

请注意,在 C++ 11 中,现在允许以下声明:

struct s {
   int i = 10;
};

这是一个老问题,但它在 Google 中排名很高,不妨澄清一下。

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.

爱你是孤单的心事 2024-07-14 21:27:27

Edit2: 这个答案写于 2008 年,与 C++98 相关。 成员初始化的规则在该语言的后续版本中发生了变化。

编辑:这个问题最初被标记为c++,但发帖者说它与c有关,所以我重新标记了问题,我留下了答案不过...

在 C++ 中,struct 只是一个 class,其成员默认为 public 而不是 private遗产。

C++ 只允许内联初始化 static const 整型成员,其他成员必须在构造函数中初始化,或者如果 structPOD 在初始化列表中(声明变量时)。

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?" };

Edit2: This answer was written in 2008 and relates to C++98. The rules for member initialization have changed in subsequent versions of the language.

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?" };
总攻大人 2024-07-14 21:27:27

我们无法初始化,因为当我们声明任何结构而不是我们实际执行的结构时,只需通知编译器它们的存在,即没有为其分配内存,并且如果我们初始化没有内存的成员。 通常,当我们初始化任何变量时会发生什么,这取决于我们声明变量的位置编译器为该变量分配内存。

int a = 10;
  • 如果它是自动的,则比在堆栈内存中分配,
  • 如果它是全局的,则比在数据段内存中分配,

那么需要什么内存来保存该数据,但在结构的情况下,没有内存,因此无法初始化它。

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.

傲世九天 2024-07-14 21:27:27

正如你所说,它只是一个成员而不是一个变量。 当您声明变量时,编译器还将为这些变量提供可以放置值的内存空间。 对于结构成员的 a ,编译器不会为其提供内存空间,因此除非创建该结构类型的变量,否则无法为结构成员赋值。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文