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

Typedef union problem

Hello! I have a typedef union and variable:

typedef union int2char{unsigned char high_byte;
                       unsigned char low_byte;
                       unsigned int  int_byte;
                      } int2char;

int2char dac_value;


When i assign

dac_value.int_byte = 0xAAAA;


i have

dac_value.int_byte = 0xAAAA;
dac_value.low_byte = 0xAA;
dac_value.high_byte = 0xAA;


but when i assign,example,0xFF05

dac_value .int_byte = 0xFF05;
dac_value.low_byte =0xFF;
dac_value.high_byte = 0xFF;


same results are obtained when i assign,example,0x1005

dac_value .int_byte = 0x1005;
dac_value.low_byte =0x10;
dac_value.high_byte = 0x10;


I can not understand where the error and why??
thanks for the help!.

Parents
  • Yes indeed the problem I have decided in two ways, should announce:

    typedef union int2char{
    unsigned int int_byte;
    unsigned char char_byte[2];}int2char;
    


    } or

    typedef union int2char{
    unsigned int int_byte;
    struct char_byte{
    unsigned char high_byte;
    unsigned char low_byte;}char_byte;
    }int2char;
    
    }
    


    Thanks a lot for such an active assistance
    also sorry for the low level of English, I use a translator. :)

Reply
  • Yes indeed the problem I have decided in two ways, should announce:

    typedef union int2char{
    unsigned int int_byte;
    unsigned char char_byte[2];}int2char;
    


    } or

    typedef union int2char{
    unsigned int int_byte;
    struct char_byte{
    unsigned char high_byte;
    unsigned char low_byte;}char_byte;
    }int2char;
    
    }
    


    Thanks a lot for such an active assistance
    also sorry for the low level of English, I use a translator. :)

Children
No data