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
  • Hello,

    Thx for your reply,

    "Returning for main() goes where?"

    Sorry, the code is not original code, just similar. So I don't use the function in main.

    This definition includes null char, MyArray has six bytes but not important.
    const char MyArray[]={"12345"};

    because my sample code is wrong. I'm not using printf in original code, I'm using union pointer memory map. Namely as following

    // BufPtr which is usart array includes 5 bytes(it is coming with gprs) so I point with
    // pointer of CreditLoadRegs_t
    // if BufPtr includes "12345", Result must be Ptr32->Select='1'; and Ptr32->Value32="5432";
    // Because of union is packed

    This code works in devcpp but don't work in keil.

    micro is stm32f103, it can be packed as byte and union is packed.

    I didn't find my fault.

    
    void CreditProc(u8 RW, char *BufPtr)
    {
       if (RW == dREAD)
       {
          // some codes
       }else{
            CreditLoadRegs_t  *Ptr32 = (CreditLoadRegs_t  *)BufPtr;
            // I'm using regs here
            Ptr32->Select &=0x03;
            switch(Ptr32->Select)
            {
               case dSel0:
                   // something
               break;
               /// ...
            }
       }
    }
    

Reply
  • Hello,

    Thx for your reply,

    "Returning for main() goes where?"

    Sorry, the code is not original code, just similar. So I don't use the function in main.

    This definition includes null char, MyArray has six bytes but not important.
    const char MyArray[]={"12345"};

    because my sample code is wrong. I'm not using printf in original code, I'm using union pointer memory map. Namely as following

    // BufPtr which is usart array includes 5 bytes(it is coming with gprs) so I point with
    // pointer of CreditLoadRegs_t
    // if BufPtr includes "12345", Result must be Ptr32->Select='1'; and Ptr32->Value32="5432";
    // Because of union is packed

    This code works in devcpp but don't work in keil.

    micro is stm32f103, it can be packed as byte and union is packed.

    I didn't find my fault.

    
    void CreditProc(u8 RW, char *BufPtr)
    {
       if (RW == dREAD)
       {
          // some codes
       }else{
            CreditLoadRegs_t  *Ptr32 = (CreditLoadRegs_t  *)BufPtr;
            // I'm using regs here
            Ptr32->Select &=0x03;
            switch(Ptr32->Select)
            {
               case dSel0:
                   // something
               break;
               /// ...
            }
       }
    }
    

Children
No data