简体   繁体   English

如何一次初始化几个struct变量?

[英]How to initialize several struct variables at once?

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

You can take an array of the struct s and use a single brace-enclosed initializer, like 你可以使用一个struct的数组并使用一个大括号括起来的初始化器,比如

 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. 151)未明确初始化的所有子对象应与具有静态存储持续时间的对象隐式初始化。

and, initialization for objects having static storage, 并且,初始化具有static存储的对象,

[...] 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; - 如果它是一个聚合,则根据这些规则初始化(递归)每个成员,并将任何填充初始化为零比特;

— 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. 也就是说,如果您不希望将所有值初始化为0 ,则可以采用其他方法。 As mentioned in the other answer by Mr. Jonathon Reinhart, you can make use of designated initializers. 正如Jonathon Reinhart先生在其他答案中所提到的,您可以使用指定的初始化器。

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. 继续使用Linux风格,无论如何你都不会有多个变量。 It makes different easier to look at when variables are added/removed. 添加/删除变量时,更容易看到它们。 What is wrong with: 出什么问题了:

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. 否则,您将需要使用常规旧循环在运行时初始化数据。

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: 不是真的“ 立刻 ”,无论这意味着什么,但至少不重复自己(即遵循DRY委托人 )你可以做到:

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;

  ...
}

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM