int main
{
uint8_t a[3] ={0x4,0x3,0x1};
uint16_t b= *((uint16_t *) a);
cout << (int)b;
}
Result :772 So what is this number 772? Thank you!
int main
{
uint8_t a[3] ={0x4,0x3,0x1};
uint16_t b= *((uint16_t *) a);
cout << (int)b;
}
Result :772 So what is this number 772? Thank you!
You need to google a bit about how array are stored in memory. When you declare
uint8_t a[3] = {0x4,0x3,0x1};
You create a block of memory that takes 3 bytes and contains 3 elements (not considering padding which is another topic).
so for
uint8_t a[3] = {0x4,0x3,0x1};
you get in memory
Byte1: 0x4
Byte2: 0x3
Byte3: 0x1
while when accessing a 16 bit variable, the compiler expects 3 elements that are each 2 bytes and take 6 bytes in total. So for
uint16_t a[3] = {0x4,0x3,0x1};
you get in memory
Byte1+2: 0x4, 0x0
Byte3+4: 0x3, 0x0
Byte5+6: 0x1, 0x0
in your case, when you cast the uint8_t array into uint16_t, you merge the 0x4 and 0x3 into 0x304 which is, as you know, 772...
Hope this helps