如何同时初始化多个结构变量?

时间:2022-12-27 22:33:07

For a struct like

这样的结构

struct data{
   int a;
   int b;
   int c;
};

how can I initialize several instances of that struct with identical values at once?

如何同时用相同的值初始化该结构的几个实例?

Instead of:

而不是:

struct data object1 = {0,0,0}, object2 = {0,0,0}, object3 = {0,0,0};

4 个解决方案

#1


3  

how can I initialize several instances of that struct with identical values at once?

如何同时用相同的值初始化该结构的几个实例?

Not really "at once", whatever this is meant to mean, but at least without repeating yourself (that is following the DRY-principal) you could do:

不是真的“马上”,无论这是什么意思,但至少不要重复你自己(那是跟着干校长)你可以做的:

int main(void)
{
  struct data object1 = {1, 2, 3}, object2 = object1, object3 = object1;

  ...
}

or with each definition on a separate line:

或与每一个定义单独行:

int main(void)
{
  struct data object1 = {1, 2, 3};
  struct data object2 = object1;
  struct data object3 = object1;

  ...
}

#2


4  

You can take an array of the structs and use a single brace-enclosed initializer, like

您可以使用结构的数组并使用单个带括起的初始化器,比如

 struct data object [3] = {0};

go have multiple variables of that structure type, all initialized to 0 (or equivalent).

go具有该结构类型的多个变量,都初始化为0(或等效)。

This makes use of a special property of initialization, quoting C11, chapter

这利用了初始化的一个特殊属性,引用C11章节

The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject;151) all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.

初始化应该按照初始化器列表的顺序进行,为一个特定的子对象提供的每个初始化器都要重写之前列出的相同子对象的初始化;

and, initialization for objects having static storage,

对具有静态存储的对象进行初始化,

[...] If an object that has static or thread storage duration is not initialized explicitly, then:

[…如果具有静态或线程存储时间的对象没有显式初始化,则:

— if it has pointer type, it is initialized to a null pointer;

-如果有指针类型,则初始化为空指针;

— if it has arithmetic type, it is initialized to (positive or unsigned) zero;

-如果具有算术类型,则初始化为(正的或无符号的)零;

— if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;

-如果是聚合,则根据这些规则对每个成员进行初始化(递归地),并将任何填充初始化为0位;

— if it is a union, the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;

-如果它是一个联合,则根据这些规则初始化(递归地)第一个命名的成员,并将任何填充初始化为零位;

That said, in case you don't want all the values to be initialized to 0, there are alternate ways. As mentioned in the other answer by Mr. Jonathon Reinhart, you can make use of designated initializers.

也就是说,如果不希望所有的值都初始化为0,有其他的方法。正如Jonathon Reinhart先生在另一个答案中提到的,您可以使用指定的初始化器。

#3


4  

First off, I tend to favor the Linux kernel style and strongly prefer designated initializers.

首先,我倾向于Linux内核风格,并强烈倾向于指定的初始化器。

I would do what they do, and create a macro for initializing your struct. That makes it easy to add elements and control how they are initialized.

我将做他们所做的,并创建一个宏来初始化你的结构。这使得添加元素和控制如何初始化变得很容易。

struct data {
   int a;
   int b;
   int c;
};

#define INIT_DATA { \
    .a = 0, \
    .b = 0, \
    .c = 0, \
}

And use it like this:

像这样使用它:

struct data mydata = INIT_DATA;

Continuing with the Linux style, you wouldn't have more than one of these variables on a line anyway. It makes different easier to look at when variables are added/removed. What is wrong with:

继续使用Linux样式,无论如何,在一行中不会有超过一个这样的变量。当变量被添加/删除时,查看它们会更容易。有什么问题:

struct data old_data = INIT_DATA;
struct data new_data = INIT_DATA;

If you have more than a couple, should they be individual variables or should they be an array? If so, you can take advantage of a GNU extension to initialize a range:

如果有多个变量,它们应该是单个变量还是数组?如果是这样,您可以利用GNU扩展来初始化一个范围:

struct data datas[N] = {
    [0 ... N-1] = INIT_DATA,
};

Otherwise, you will need to use a regular old loop to initialize your data at run-time.

否则,您将需要使用一个常规的旧循环来在运行时初始化数据。

#4


0  

Create an array of structures and initialize them in a loop.

创建一个结构数组并在循环中初始化它们。

struct data array[N_ITEMS];

for(i=0; i<N_ITEMS; i++)
{
    array[i].a=a;
    array[i].b=b;
    array[i].c=c;
}

If you want to initialize all fields to 0, you can use memset:

如果要将所有字段初始化为0,可以使用memset:

 memset(array, 0, sizeof(array));

#1


3  

how can I initialize several instances of that struct with identical values at once?

如何同时用相同的值初始化该结构的几个实例?

Not really "at once", whatever this is meant to mean, but at least without repeating yourself (that is following the DRY-principal) you could do:

不是真的“马上”,无论这是什么意思,但至少不要重复你自己(那是跟着干校长)你可以做的:

int main(void)
{
  struct data object1 = {1, 2, 3}, object2 = object1, object3 = object1;

  ...
}

or with each definition on a separate line:

或与每一个定义单独行:

int main(void)
{
  struct data object1 = {1, 2, 3};
  struct data object2 = object1;
  struct data object3 = object1;

  ...
}

#2


4  

You can take an array of the structs and use a single brace-enclosed initializer, like

您可以使用结构的数组并使用单个带括起的初始化器,比如

 struct data object [3] = {0};

go have multiple variables of that structure type, all initialized to 0 (or equivalent).

go具有该结构类型的多个变量,都初始化为0(或等效)。

This makes use of a special property of initialization, quoting C11, chapter

这利用了初始化的一个特殊属性,引用C11章节

The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject;151) all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.

初始化应该按照初始化器列表的顺序进行,为一个特定的子对象提供的每个初始化器都要重写之前列出的相同子对象的初始化;

and, initialization for objects having static storage,

对具有静态存储的对象进行初始化,

[...] If an object that has static or thread storage duration is not initialized explicitly, then:

[…如果具有静态或线程存储时间的对象没有显式初始化,则:

— if it has pointer type, it is initialized to a null pointer;

-如果有指针类型,则初始化为空指针;

— if it has arithmetic type, it is initialized to (positive or unsigned) zero;

-如果具有算术类型,则初始化为(正的或无符号的)零;

— if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;

-如果是聚合,则根据这些规则对每个成员进行初始化(递归地),并将任何填充初始化为0位;

— if it is a union, the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;

-如果它是一个联合,则根据这些规则初始化(递归地)第一个命名的成员,并将任何填充初始化为零位;

That said, in case you don't want all the values to be initialized to 0, there are alternate ways. As mentioned in the other answer by Mr. Jonathon Reinhart, you can make use of designated initializers.

也就是说,如果不希望所有的值都初始化为0,有其他的方法。正如Jonathon Reinhart先生在另一个答案中提到的,您可以使用指定的初始化器。

#3


4  

First off, I tend to favor the Linux kernel style and strongly prefer designated initializers.

首先,我倾向于Linux内核风格,并强烈倾向于指定的初始化器。

I would do what they do, and create a macro for initializing your struct. That makes it easy to add elements and control how they are initialized.

我将做他们所做的,并创建一个宏来初始化你的结构。这使得添加元素和控制如何初始化变得很容易。

struct data {
   int a;
   int b;
   int c;
};

#define INIT_DATA { \
    .a = 0, \
    .b = 0, \
    .c = 0, \
}

And use it like this:

像这样使用它:

struct data mydata = INIT_DATA;

Continuing with the Linux style, you wouldn't have more than one of these variables on a line anyway. It makes different easier to look at when variables are added/removed. What is wrong with:

继续使用Linux样式,无论如何,在一行中不会有超过一个这样的变量。当变量被添加/删除时,查看它们会更容易。有什么问题:

struct data old_data = INIT_DATA;
struct data new_data = INIT_DATA;

If you have more than a couple, should they be individual variables or should they be an array? If so, you can take advantage of a GNU extension to initialize a range:

如果有多个变量,它们应该是单个变量还是数组?如果是这样,您可以利用GNU扩展来初始化一个范围:

struct data datas[N] = {
    [0 ... N-1] = INIT_DATA,
};

Otherwise, you will need to use a regular old loop to initialize your data at run-time.

否则,您将需要使用一个常规的旧循环来在运行时初始化数据。

#4


0  

Create an array of structures and initialize them in a loop.

创建一个结构数组并在循环中初始化它们。

struct data array[N_ITEMS];

for(i=0; i<N_ITEMS; i++)
{
    array[i].a=a;
    array[i].b=b;
    array[i].c=c;
}

If you want to initialize all fields to 0, you can use memset:

如果要将所有字段初始化为0,可以使用memset:

 memset(array, 0, sizeof(array));