Can you get a pointer to the low byte in a way independent of you?

I have a 16 bit variable data, that is:

volatile uint16_t data;

I need to fill in this value based on the contents of two 8-bit registers on an external sensor. Access to them is via I2C / TWI.

My TWI routine is async * and has a signature:

bool twi_read_register(uint8_t sla, uint8_t reg, uint8_t *data, void (*callback)(void));

It reads the value regon slaat *data, then calls callback().

If I knew what uint16_twas placed in memory, like, say MSB LSB, I could do:

twi_read_register(SLA, REG_MSB, (uint8_t *)&data, NULL);
twi_read_register(SLA, REG_LSB, (uint8_t *)&data + 1, NULL);

However, I do not like to bake endian dependency in my program. Is there a way to achieve this in a way that is independent of you?

(side note: my actual workaround currently involves using a structure, i.e.:

typedef struct {
    uint8_t msb;
    uint8_t lsb;
} SensorReading;

, uint16_t)

(* async split-phase, .. *data - , callback, )

+5
3

?

uint8_t v1, v2;
twi_read_register(SLA, REG_MSB, &v1, NULL);
twi_read_register(SLA, REG_LSB, &v2, NULL);
data = ((uint16_t)v1<<8)|v2;

data , twi_read_register . , , .

, data , . , , , , . , , .

, .

#ifdef BIGENDIAN
typedef struct
{       uint8_t  msb, lsb;
} uint16_as_uint8_t;
#else
typedef struct
{       uint8_t  lsb, msb;
} uint16_as_uint8_t;
#endif

, union

union
{       uint16_as_uint8_t  as8;
        uint16_t           as16;
};

, C89, union , . C99 ( ) . C89 (char*), .

, , , - . , . , , , , , , , , . .

+4

- ?

uint16_t myValue = ...;
uint8_t LSB = (uint8_t)(myValue % 256);
uint8_t MSB = (uint8_t)(myValue / 256);
0
  volatile uint16_t data;  
  uint16_t v = 1;
  twi_read_register(SLA, REG_MSB, (uint8_t *)&data + *((uint8_t *)&v), NULL);
  twi_read_register(SLA, REG_LSB, (uint8_t *)&data + *((uint8_t *)&v + 1), NULL);
0
source