This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

What mistake I am doing here in C code

I wrote following code


typedef union
{
    struct
    {
        int8_t   a;
        int16_t  b;
        uint32_t c;
    };

    int8_t my_arr[7];
}x;

uint8_t arr[7];

int main( void )
{
    uint32_t i;
    x *y;

    for( i = 0 ; i < 7 ; i++ )
    {
        arr[i] = i + 2 * 5;
    }

    y = ( x* )arr;

    i = y->a;
    i = y->b;
    i = y->c;
}



End result in Y is:
a = 0x0a;
b = 0x0d0c
c = 0x00100F0E
my_arr[0-7] = 0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10;

& array conatisn
arr[0-7] = 0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10;

My question is when a= 0x0a , then should be b = 0x0c0b & c = 0x100f0e0d
but this is not the case

I am using keil 4.70.0.0, mdk arm

  • Are you not getting compilation error?

    union doesnt have 'a', 'b' or 'c'. union has structure which has these fields. and you are trying to access fields that are not a part of union.
    y->a

  • Try PACK Compiler Directive

    Default PACK(2)

    The PACK directive causes the compiler to generate byte-aligned or half-word-aligned structures. This is useful when exchanging data with other systems where no alignment is required.

    Example

    #pragma pack(1)     /* byte alignment */
    #pragma bytealign   /* ptrs to byte-aligned objects */
    
    struct s1  {
      int  i1;     // i1 has offset 0
      char c1;     // c1 has offset 2
      int  i2;     // i2 has offset 3
      char c2;     // c2 has offset 5
      int  i3;     // i3 has offset 6
      char z1;     // z1 has offset 8
    };
    
    
    #pragma pack()    /* reset to default alignment */
    
    struct s2  {
      int  i1;     // i1 has offset 0
      char c1;     // c1 has offset 2
      int  i2;     // i2 has offset 4
      char c2;     // c2 has offset 6
      int  i3;     // i3 has offset 8
      char z1;     // z1 has offset 10
    };
    
    

  • Thanks it has working now with pack instruction.

    But I have one more structure with mixture of float

    At point of code it is giving error:


    argument of type "__packed_float *" is incompatible with parameter of type "float *"

    Also what is defalut value n in #pragme pack(n).
    Help tpic says its 8, where n is no of bytes.

    But I think it should be 4 & incorrectly written their. As 4 bytes alignment will be default

  • Try PACK Compiler Directive

    Or better still: don't. Numerous people keep suggesting packed data structures as the solution to this "problem", and the suggestion usually comes so quickly and with so little backing argument that it's clear it's really a reflex rather than a considered strategy. Not surprisingly, given this lack of reasoning, the suggested solution is usually wrong.

    The real solution is to realize that this is not a problem in the first place, but just a simple case of misguided expectations. The compiler had good reasons not to pack that structure in the first place. It takes a very strong reason to justify overriding it. And if you do, you have to know exactly what the consequences will be, and how you'll live with those.

    But most of the time, you should just get rid of that unwarranted expectation.

  • Try PACK Compiler Directive

    Or better still: don't. Numerous people keep suggesting packed data structures as the solution to this "problem", and the suggestion usually comes so quickly and with so little backing argument that it's clear it's really a reflex rather than a considered strategy. Not surprisingly, given this lack of reasoning, the suggested solution is usually wrong.

    TOTALLY agree.

    I have stated to many "pack should ONLY be used when importing data files where the data is "packed" and using the pack directive makes the data 'slip' into a structure.

  • Or better still: don't.

    I would concur. It's nice to see that everyone, once in a while, can write something sensible.