在声明时初始化一个联合数组

时间:2022-07-05 21:26:42

I'm trying to initialize the following union array at declaration:

我正在尝试在声明时初始化以下union数组:

typedef union { __m128d m;  float f[4]; } mat;
mat m[2] = { {{30467.14153,5910.1427,15846.23837,7271.22705},
{30467.14153,5910.1427,15846.23837,7271.22705}}};

But I'getting the following error:

但是我发现了以下错误:

matrix.c: In function ‘main’:
matrix.c:21: error: incompatible types in initialization
matrix.c:21: warning: excess elements in union initializer
matrix.c:21: warning: (near initialization for ‘m[0]’)
matrix.c:21: warning: excess elements in union initializer
matrix.c:21: warning: (near initialization for ‘m[0]’)
matrix.c:21: warning: excess elements in union initializer
matrix.c:21: warning: (near initialization for ‘m[0]’)
matrix.c:21: error: incompatible types in initialization
matrix.c:21: warning: excess elements in union initializer
matrix.c:21: warning: (near initialization for ‘m[1]’)
matrix.c:21: warning: excess elements in union initializer
matrix.c:21: warning: (near initialization for ‘m[1]’)
matrix.c:21: warning: excess elements in union initializer
matrix.c:21: warning: (near initialization for ‘m[1]’)

3 个解决方案

#1


11  

Quoting this page:

引用此页面:

With C89-style initializers, structure members must be initialized in the order declared, and only the first member of a union can be initialized.

使用C89样式的初始值设定项,必须按声明的顺序初始化结构成员,并且只能初始化union的第一个成员。

So, either put the float array first, or if possible use C99 and write:

所以,要么先将float数组放入,要么尽可能使用C99并写:

mat m[2] = { { .f = { /* and so on */ } }, /* ... */ };

The important thing being the .f.

重要的是.f。

#2


2  

You need to indicate which union field you are initializing. Try using this syntax:

您需要指明要初始化的联合字段。尝试使用以下语法:

mat m[2] = {
    {.f = {30467.14153,5910.1427,15846.23837,7271.22705}},
    {.f = {30467.14153,5910.1427,15846.23837,7271.22705}}
};

This successfully compiled for me, without any warnings.

这成功为我编译,没有任何警告。

#3


0  

Try to change members:

尝试更改成员:

typedef union {
    float f[4];
    __m128d m;
} mat;
mat m[2] = { { {30467.14153,5910.1427,15846.23837,7271.22705},
               {30467.14153,5910.1427,15846.23837,7271.22705} } };

If you initialize union without member specification like .f = { ... } then first member of union is initialized.

如果初始化union而没有成员规范,如.f = {...},则初始化union的第一个成员。

#1


11  

Quoting this page:

引用此页面:

With C89-style initializers, structure members must be initialized in the order declared, and only the first member of a union can be initialized.

使用C89样式的初始值设定项,必须按声明的顺序初始化结构成员,并且只能初始化union的第一个成员。

So, either put the float array first, or if possible use C99 and write:

所以,要么先将float数组放入,要么尽可能使用C99并写:

mat m[2] = { { .f = { /* and so on */ } }, /* ... */ };

The important thing being the .f.

重要的是.f。

#2


2  

You need to indicate which union field you are initializing. Try using this syntax:

您需要指明要初始化的联合字段。尝试使用以下语法:

mat m[2] = {
    {.f = {30467.14153,5910.1427,15846.23837,7271.22705}},
    {.f = {30467.14153,5910.1427,15846.23837,7271.22705}}
};

This successfully compiled for me, without any warnings.

这成功为我编译,没有任何警告。

#3


0  

Try to change members:

尝试更改成员:

typedef union {
    float f[4];
    __m128d m;
} mat;
mat m[2] = { { {30467.14153,5910.1427,15846.23837,7271.22705},
               {30467.14153,5910.1427,15846.23837,7271.22705} } };

If you initialize union without member specification like .f = { ... } then first member of union is initialized.

如果初始化union而没有成员规范,如.f = {...},则初始化union的第一个成员。