How to initialize union items

Hello

Can any body tell me how can i initialize the unsigned int urow item which is an array of 16 elements in union myun as following?

i can use any of the following 2 declarations if i can find out how to initialize urow item in it.

union myun
{
  unsigned int urow;
  struct myst
  {
    unsigned int p32  : 1;   unsigned int p31  : 1;   unsigned int p30  : 1;   unsigned int p29  : 1;
    unsigned int p28  : 1;   unsigned int p27  : 1;   unsigned int p26  : 1;   unsigned int p25  : 1;
    unsigned int p24  : 1;   unsigned int p23  : 1;   unsigned int p22  : 1;   unsigned int p21  : 1;
    unsigned int p20  : 1;   unsigned int p19  : 1;   unsigned int p18  : 1;   unsigned int p17  : 1;
    unsigned int p16  : 1;   unsigned int p15  : 1;   unsigned int p14  : 1;   unsigned int p13  : 1;
    unsigned int p12  : 1;   unsigned int p11  : 1;   unsigned int p10  : 1;   unsigned int p9   : 1;
    unsigned int p8   : 1;   unsigned int p7   : 1;   unsigned int p6   : 1;   unsigned int p5   : 1;
    unsigned int p4   : 1;   unsigned int p3   : 1;   unsigned int p2   : 1;   unsigned int p1   : 1;
  }ss;
}myunion[16];

OR

union myun
{
  unsigned int urow[16];
  struct myst
  {
    unsigned int p32  : 1;   unsigned int p31  : 1;   unsigned int p30  : 1;   unsigned int p29  : 1;
    unsigned int p28  : 1;   unsigned int p27  : 1;   unsigned int p26  : 1;   unsigned int p25  : 1;
    unsigned int p24  : 1;   unsigned int p23  : 1;   unsigned int p22  : 1;   unsigned int p21  : 1;
    unsigned int p20  : 1;   unsigned int p19  : 1;   unsigned int p18  : 1;   unsigned int p17  : 1;
    unsigned int p16  : 1;   unsigned int p15  : 1;   unsigned int p14  : 1;   unsigned int p13  : 1;
    unsigned int p12  : 1;   unsigned int p11  : 1;   unsigned int p10  : 1;   unsigned int p9   : 1;
    unsigned int p8   : 1;   unsigned int p7   : 1;   unsigned int p6   : 1;   unsigned int p5   : 1;
    unsigned int p4   : 1;   unsigned int p3   : 1;   unsigned int p2   : 1;   unsigned int p1   : 1;
  }ss[16];
}myunion;

similar to this question here

sorry no.
here i am asking about initializing the urow element.
while in the post of your link the matter ws different, it was about difference between the two types of declarations, union of array or array of union.

If you have a union:

union
{
    byte this[4];
    float that;
} stuff;

You need to create an instance of the union:

   stuff crap;

Then, you can initialize either part of the union:

   crap.this[0] = 12;
   crap.this[1] = 45;
   crap.this[2] = 0;
   crap.this[3] = 17;

OR

   crap.that = 3.14159;

what's really the question? you just access it like you would do with any union member.

myunion.urow = 10;

Actually i am trying to initialize in this way.

crap.this[4] = {1,2,3,4}:
is something like this possible?

specially at the time of declaration.

is something like this possible?

No. Once an array is defined, you MUST provide values one element at a time.

The only way that you can initialize an array with a list of values is by providing them at the same time that the array is defined.

i tried that inside union declaration but it does not work.
Can you give me an example?

i tried that inside union declaration but it does not work.

Of course it doesn't, because you are not allocating memory at the same time. A union is nothing more than a recipe. It allocates no memory.

The array is actually allocated when the union is instantiated.

// When initializing a union, the initializer list must have only one member,
// which initializes the first member of the union
// unless a designated initializer is used (since C99).

union {
  uint32_t x;
  char c[4];
}
u1 = {12345},                       // makes u1.x active with value 12345
u2 = { .c = {'1', '2', '3', '4'} }; // makes u2.c active with value {'1','2','3','4'}


void setup() {
  Serial.begin(115200);
  Serial.println(u1.x);
  for (int i = 0; i < 4; i++) Serial.println(u2.c[i]);

  // Note that this is not legit and compiler dependant as member not the one used for initialization
  Serial.println(u2.x);
  for (int i = 0; i < 4; i++) Serial.println(u1.c[i]);
}

void loop() {}

J-M-L:

// When initializing a union, the initializer list must have only one member,

// which initializes the first member of the union
// unless a designated initializer is used (since C99).

maybe an example including the array OP started with:

union MyUnion
{
  unsigned int myInt;
  byte myByte[2];
}myunion[2] =
  {
    0xFFFF,
    0xFEFE
  };

void setup() 
{
  Serial.begin(9600);
  Serial.println(myunion[1].myByte[0], HEX);
}

void loop() {
  // put your main code here, to run repeatedly:

}

added: When initializing a union, the initializer list must have only one member, which initializes the first member of the union unless a designated initializer is used

J-M-L how to use your method if the union declaration is as this:

union {
  uint32_t x;
  char c;
}u1[4]

this is equivalent to:

char c[4]

inside the union.

So any idea how to initialize the element c in the above union declaration?

this is equivalent to...inside the union.

It most certainly is not.

i think following 2 are same:

union u1
{
...
char c[4];
...
}myu1;

And

union u1
{
...
char c;
...
}myu1[4];

Does both the above declaration generates same c[4] array?
And how to initialize c in both cases at the time of union declaration?

naeemdotcom:
Does both the above declaration generates same c[4] array?
And how to initialize c in both cases at the time of union declaration?

it was just shown to you.... just move "c" to the top of the list of elements in the union!

union MyUnion
{
  byte myByte[2];
  unsigned int myInt;
  
}myunion[2] =
  {
  {0xFF, 0xFF}, {0xFE, 0xFE}
  };

void setup() 
{
  Serial.begin(9600);
  Serial.println(myunion[0].myByte[0], HEX);
}

void loop() {
  // put your main code here, to run repeatedly:

}

Does both the above declaration generates same c[4] array?

No. Only one defines an array with 4 elements that is unioned with something else. The other one defines a single char, and 4 instances of the union.

You will NOT be able to use myu1.c[2] with the second instance.