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

and again the question of variables

There is an SRC variable of 16 bits, how to create two 8-bit variables, so that one is addressed to the upper part and the second to the lower part of the SRC variable?
type construction

uint16_t src;
uint8_t b1 = * ((uint8_t *) & src);
uint8_t b2 = * (((uint8_t *) & src) +1);


should work but at compilation produces:

MAIN.c (16): error: # 28: expression must have a constant value
  uint8_t b1 = * ((uint8_t *) & src);
MAIN.c (17): error: # 28: expression must have a constant value
  uint8_t b2 = * (((uint8_t *) & src) +1);
MAIN.c: 0 warnings, 2 erros


How to correctly address the variables B1 and B2 that, when changing these variables, the corresponding value in the SRC variable and vice versa change.
Sorry for my english :)

Parents
  • Hello, THANK YOU to everyone who helped solve my question :) After the last clue, I solved the task posed :)
    Here's the code I'm currently using:

    union Register32 {
         struct {
             uint8_t byte1;
             uint8_t byte2;
             uint8_t byte3;
             uint8_t byte4;
         } bytes;
         struct {
             uint16_t low;
             uint16_t high;
         } words;
         uint32_t dword;
    };
    
    typedef union Register32 EAX;
    
    int main (void) {
    EAX pw1;
    pw1.dword = 0xaaddC0FF;
    }
    

Reply
  • Hello, THANK YOU to everyone who helped solve my question :) After the last clue, I solved the task posed :)
    Here's the code I'm currently using:

    union Register32 {
         struct {
             uint8_t byte1;
             uint8_t byte2;
             uint8_t byte3;
             uint8_t byte4;
         } bytes;
         struct {
             uint16_t low;
             uint16_t high;
         } words;
         uint32_t dword;
    };
    
    typedef union Register32 EAX;
    
    int main (void) {
    EAX pw1;
    pw1.dword = 0xaaddC0FF;
    }
    

Children
No data