array of union or union of array

Hi All, Can some body help me in understanding the differences between following 2 declarations?

1.

union u2
{
 unsigned long urow2;
 struct s2
 {
   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;
 }ss2;
}myunion2[16];

2.

union u2
{
 unsigned long urow2[16];
 struct s2
 {
   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;
 }ss2[16];
}myunion2;

Basically i want to initialize the unsigned int urow2 4-byte variable and then access each of its bits one by one. Which of the above 2 declarations is suitable for doing this?

Any help would be great.

  1. Follow the forum rules and ALLWAYS post code in code tags!

  2. If you are writing things like this (note the code tags)

  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;
  etc

then you should be using an array! There are no excuses!

  1. Struct's have nothing to do with this. You should have used a 3D array.

  2. In software we always number from 0!

  3. Just how much SRAM do you think you have? What is 16*16*32*2?

Mark

Mark

naeemdotcom: Basically i want to initialize the unsigned int urow2 4-byte variable and then access each of its bits one by one. Which of the above 2 declarations is suitable for doing this?

If you use an 8-bit AVR, both are wrong.

The first uses one two byte variable overlaid with a 32 bit value, 16 times.

The second uses 16 two byte variables overlaid with 16 32 bit values.

Use an unsigned long for urow2 (and the first layout).

holmes4: 2. If you are writing things like this (note the code tags)

  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;
  etc

then you should be using an array! There are no excuses!

No. There are no arrays of bit-fields.

holmes4: 3. Struct's have nothing to do with this. You should have used a 3D array.

No and no.

holmes4: 5. Just how much SRAM do you think you have? What is 16*16*32*2?

The OP only needs 16*4 bytes with an array of unions.

yes i mean 32 bit (4-byte) variable which is unsigned long. i have corrected that in the original post now.

You should not change posted code, but post the new version (in code tags).

Opps

If you make urow 32 bits the memory layout of both is identical, it is just a matter of moving the index to a different entity.

yes i need 16 4-byte long variables.

i can initialize all those 16 variables as unsigned long variables. but at the same time i want to access each of the bit of 16 variables separately, one by one. How can i do that using union or some other way?

You could use logical bit tests on elements of a simple array.

You could use bitRead() on elements of a simple array.

You could use an array of unions and access longs or bits with array and union syntax.

You could use a union of arrays and access longs or bits with union and array syntax.

You can have:

myUnion2[i].ss2.p25
or
myUnion2.ss2[i].p25

Either way will work. I think it would be easier to read to have the union represent one unsigned long and make an array of 16 of them (your first example).