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

union/struct problem

Hello,

it works in devcpp (as c project) but not working in Keil. What is my fault.
output must be "12345" but it is not.

typedef union {
        u8      Reg8[5];
        struct
        {
                u8      Select;
                u32     Value32;
        }Regs;
}CreditLoadRegs_t;


const char  MyArray[]={"12345"};

void MyFonc(char *Buf)
{
    CreditLoadRegs_t *fPtr = (CreditLoadRegs_t *)Buf;
    printf("%s",fPtr->Reg8);
}

int main(void)
{
    MyFonc(MyArray);

    return 0;
}

Parents
  • What is my fault.

    Mainly that you're trying to run a 400 meter race before you've fully mastered walking.

    output must be "12345"

    Based on what promises made by the C programming language's syntax and semantics, and/or compiler documention, did you arrive at that belief?

    According to my knowledge about those documents, this code causes undefined behaviour for two reasons: 1) bad pointer gymnastics, 2) abuse of a union.

Reply
  • What is my fault.

    Mainly that you're trying to run a 400 meter race before you've fully mastered walking.

    output must be "12345"

    Based on what promises made by the C programming language's syntax and semantics, and/or compiler documention, did you arrive at that belief?

    According to my knowledge about those documents, this code causes undefined behaviour for two reasons: 1) bad pointer gymnastics, 2) abuse of a union.

Children