Initialize union array on declaration

I am trying to initialize the following union array in a declaration:

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 gave 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]') 
+6
source share
3 answers

Quote this page :

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

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

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

The important thing is .f .

+11
source

You need to indicate which join 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 compiled successfully for me without any warnings.

+2
source

Try changing 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 a join without a member specification, for example .f = {...}, then the first member of the join is initialized.

0
source

Source: https://habr.com/ru/post/920875/


All Articles