I have little dilemma with my code.
I have function which returns pointer (uint8_t*) to data buffer. Is it possible and legal to cast this pointer to uint16_t when I need 2 values from array as uint16_t? Or there will be problem with aligning? Buffer is filled via byte reading from I2C.
The problem exists on MCU but when I try this with visualstudio, it´s ok. MCU - Hardfault
uint8_t arr[8];
uint8_t * FuncReturnPointer(void)
{
arr[0] = 0xEE;
arr[1] = 0xFF;
return arr;
}
main
{
uint16_t val16 = 0;
val16 = *(uint16_t*)FuncReturnPointer();
}
Is there something wrong/dangerous?
char*
and alignment requirements are satisfied. The general advice would be to "manually" compose the resultinguint16_t
such as(arr[1] << 8) + arr[0]
to avoid this and possible endianess issues. – Eugene Sh.int main(void)
at least .... – alkarr
is global. – Eugene Sh.