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
  • C strings require a terminating NUL character, "12345" doesn't fit in 5 characters, and printf() is going to malfunction if the NUL is missing.

    The code as presented might work.

    If you don't get "12345" *what* do you actually get, this might be far more helpful information than what is expected.

    Returning for main() goes where?

    If you don't provide retargeting/semihosting the printf() will likely fault due to lack of supporting code.

Reply
  • C strings require a terminating NUL character, "12345" doesn't fit in 5 characters, and printf() is going to malfunction if the NUL is missing.

    The code as presented might work.

    If you don't get "12345" *what* do you actually get, this might be far more helpful information than what is expected.

    Returning for main() goes where?

    If you don't provide retargeting/semihosting the printf() will likely fault due to lack of supporting code.

Children