初始化一组结构体

时间:2022-01-01 00:59:32

I am trying to initialize an array of structs to zero. For some reason, I am getting the error "expected expression". What's wrong with my code?

我试图将一个结构数组初始化为0。由于某些原因,我得到了“预期表达式”的错误。我的代码有什么问题?

struct mystruct {
    double a;
    double arr[2];
}

int main() {
    struct mystruct *array = (struct mystruct*)malloc(3 * sizeof(struct mystruct));
    for (int i = 0; i < 3; i++) {
        array[i] = { 0 };
    }
    return 0;
}

2 个解决方案

#1


2  

Your code has a syntax error. There are different ways to correct it:

您的代码有一个语法错误。有不同的纠正方法:

  • Using the C99 syntax for compound literals:

    使用C99语法进行复合文字:

    array[i] = (struct mystruct){ 0 };
    
  • Using a separate structure with the default values:

    使用具有默认值的独立结构:

    struct mystruct def = { 0 };
    ...
    array[i] = def;
    
  • Initializing each member explicitly:

    初始化每个成员明确:

    array[i].a = 0;
    array[i].arr[0] = 0;
    array[i].arr[1] = 0;
    
  • Using calloc():

    使用calloc():

    struct mystruct *array = calloc(3, sizeof(*array));
    

    calloc() initializes the memory block to all bits zero. If your system uses IEEE representation for double, all bits zero corresponds to the value 0.0. If it does not, I will give you one dollar.

    calloc()初始化内存块到所有位为零。如果您的系统使用IEEE表示为双,所有位零对应的值为0.0。如果没有,我会给你一美元。

#2


3  

Aggregate initialization is only available for, well... initialization:

聚合初始化只能用于,嗯…初始化:

struct mystruct x = {0};   // initialization

There's no such grammar for assignment.

作业中没有这样的语法。

But you don't need to, just use calloc instead of malloc for dynamically allocated, zeroed memory.

但是您不需要这样做,只需使用calloc而不是malloc来进行动态分配、调零内存。

struct mystruct* array = calloc(3, sizeof(struct mystruct));

#1


2  

Your code has a syntax error. There are different ways to correct it:

您的代码有一个语法错误。有不同的纠正方法:

  • Using the C99 syntax for compound literals:

    使用C99语法进行复合文字:

    array[i] = (struct mystruct){ 0 };
    
  • Using a separate structure with the default values:

    使用具有默认值的独立结构:

    struct mystruct def = { 0 };
    ...
    array[i] = def;
    
  • Initializing each member explicitly:

    初始化每个成员明确:

    array[i].a = 0;
    array[i].arr[0] = 0;
    array[i].arr[1] = 0;
    
  • Using calloc():

    使用calloc():

    struct mystruct *array = calloc(3, sizeof(*array));
    

    calloc() initializes the memory block to all bits zero. If your system uses IEEE representation for double, all bits zero corresponds to the value 0.0. If it does not, I will give you one dollar.

    calloc()初始化内存块到所有位为零。如果您的系统使用IEEE表示为双,所有位零对应的值为0.0。如果没有,我会给你一美元。

#2


3  

Aggregate initialization is only available for, well... initialization:

聚合初始化只能用于,嗯…初始化:

struct mystruct x = {0};   // initialization

There's no such grammar for assignment.

作业中没有这样的语法。

But you don't need to, just use calloc instead of malloc for dynamically allocated, zeroed memory.

但是您不需要这样做,只需使用calloc而不是malloc来进行动态分配、调零内存。

struct mystruct* array = calloc(3, sizeof(struct mystruct));