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

Parents
  • 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.

Reply
  • 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.

Children
  • 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.