How does the superstructure of structures within unions work?

I have the following:

#include <stdio.h>

typedef union u_data
{
        struct
        {
                int a;
                int b;
                int c;
        };
                int elem[3];
}       my_data;

int     main(void)
{
        my_data data;

        data.a = 3;
        data.b = 5;
        data.c = -3;
        printf("%d, %d, %d\n", data.elem[0], data.elem[1], data.elem[2]);
}

and it works as I expected with the output: 3, 5, -3

however, I understand that there may be additions to them in the structure, does this mean that the elements in the structure may not always coincide with the array?

+4
source share
4 answers
  • First of all, there is a special rule "general initial sequence" for joins in C, C11 6.5.2.3:

    : , (. ), , , .

    , . , .

  • , , , -, . , .

  • C ( ++), C11 6.5.2.3/3, , . , .

  • " " .

: , . , . .

+3

, a, b c elem - , .

, , C.

+2

struct .


,

, , , , struct.

, ?

, struct .

0

, ?

.

Undefined .

elem (- ) .

-2

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


All Articles