简体   繁体   English

c 结构数组初始化

[英]c structure array initializing

I have structure我有结构

struct ABC {
  int a; 
  int b;
}

and array of it as和它的数组

struct ABC xyz[100];

I want to initialize it a = 10 and b = 20;我想初始化它 a = 10 and b = 20; for all array element.对于所有数组元素。

Which is better way?哪种方法更好?

While there is no particularly elegant way to initialize a big array like this in C, it is possible. 虽然没有特别优雅的方法在C中初始化这样的大数组,但这是可能的。 You do not have to do it in runtime, as some answers falsely claim. 不必在运行时这样做,因为一些答案错误地声称。 And you don't want to do it in runtime, suppose the array is const ? 并且你不想在运行时这样做,假设数组是const

The way I do it is by defining a number of macros: 我这样做的方法是定义一些宏:

struct ABC {
  int a; 
  int b;
};

#define ABC_INIT_100 ABC_INIT_50 ABC_INIT_50
#define ABC_INIT_50  ABC_INIT_10 ABC_INIT_10 ABC_INIT_10 ABC_INIT_10 ABC_INIT_10
#define ABC_INIT_10  ABC_INIT_2 ABC_INIT_2 ABC_INIT_2 ABC_INIT_2 ABC_INIT_2
#define ABC_INIT_2   ABC_INIT_1 ABC_INIT_1
#define ABC_INIT_1   {10, 20},

int main()
{
  struct ABC xyz[100] =
  {
    ABC_INIT_100
  };
}

Note that macros like these can be combined in any way, to make any number of initializations. 请注意,可以以任何方式组合这些宏,以进行任意数量的初始化。 For example: 例如:

#define ABC_INIT_152 ABC_INIT_100 ABC_INIT_50 ABC_INIT_2

With GCC you can use its extended syntax and do: 使用GCC,您可以使用其扩展语法并执行:

struct ABC xyz[100] = { [0 ... 99].a = 10, [0 ... 99].b = 20 };

For a portable solution I'd probably initialize one instance, and use a loop to copy that instance to the rest: 对于可移植的解决方案,我可能会初始化一个实例,并使用循环将该实例复制到其余实例:

struct ABC xyz[100] = { [0].a = 10, [0].b = 20 };

for(size_t i = 1; i < sizeof xyz / sizeof *xyz; ++i)
  xyz[i] = xyz[0];

This is somewhat cleaner to me than having the actual values in the loop. 这对我来说比在循环中具有实际值更干净。 It can be said to express the desired outcome at a slightly higher level. 可以说,在稍高的水平上表达了期望的结果。

The above syntax ( [0].a and [0].b ) is not an extension, it's typical C99. 上面的语法( [0].a[0].b不是扩展名,它是典型的C99。

for(unsigned int i=0; i<100; i++)
{
    xyz[i].a = 10;
    xyz[i].b = 20;
}

There's no explicit language support for initializing all the elements in an array of substructures to specific, non-zero default values, in the the way there is for initializing all elements to zero; 没有明确的语言支持将子结构数组中的所有元素初始化为特定的非零默认值,就像将所有元素初始化为零的方式一样; you either have to initialize each element explicitly in the source at compile-time or you have to write a for() loop and initialize each element at startup. 您必须在编译时在源中显式初始化每个元素,或者您必须编写for()循环并在启动时初始化每个元素。

As user @lundin points out in another answer, you can use preprocessor macros to reduce the typing involved in explicitly initializing those values, but as far as the C compiler is concerned, you're still initializing each element explicitly. 正如用户@lundin在另一个答案中指出的那样,您可以使用预处理器宏来减少显式初始化这些值所涉及的类型,但就C编译器而言,您仍然显式初始化每个元素。

I'm not a c expert (so when you come for my blood...)我不是 c 专家(所以当你来为我献血时......)

This complies and works fine!这符合并且工作正常!

typedef struct
{
    double var1;
    double var2;
    double var3;
    double var4;
}VectorData;

#define MAX_INDEX 100

VectorData Data[] = {[0 ... MAX_INDEX] = {0.0, 0.0, 0.0, 0.0}};

This allows you to imply the array size from MAX_INDEX.这允许您暗示来自 MAX_INDEX 的数组大小。 In this case you have an array with 101 elements.在本例中,您有一个包含 101 个元素的数组。

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

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