I have a uint8_t
array. I sometimes need to treat two sequential elements as uint16_t
, and copy them into another uint16_t
variable. The elements may not necessarily start on a uint16_t
word boundary.
At present I'm using memcpy to do so:
uint8_t bytes[] = { 0x1A, 0x2B, 0x3C, 0x4D };
uint16_t word = 0;
memcpy(&word, &bytes[1], sizeof(word));
gdb shows this works as expected:
(gdb) x/2bx &word
0x7fffffffe2e2: 0x2b 0x3c
Casting a reference to an array element to uint16_t
causes only the array element being directly referenced to be copied:
word = (uint16_t)bytes[1];
(gdb) x/2bx &word
0x7fffffffe2e2: 0x2b 0x00
Using a uint16_t
pointer and pointing it at an array element address cast to uint16_t *
results in two sequential elements being referenced, but not copied:
uint16_t *wordp = (uint16_t *)&bytes[1];
(gdb) x/2bx wordp
0x7fffffffe2e5: 0x2b 0x3c
bytes[1] = 0x5E;
(gdb) x/2bx wordp
0x7fffffffe2e5: 0x5e 0x3c
Assigning a regular uint16_t
variable the value dereferenced from the uint16_t
pointer copies both sequential array elements:
word = *wordp;
bytes[1] = 0x6F;
(gdb) x/2bx wordp
0x7fffffffe2e5: 0x6f 0x3c
(gdb) x/2bx &word
0x7fffffffe2e2: 0x5e 0x3c
gcc produces no warnings when using the -Wall
option.
How can I achieve the same result without the intermediate pointer?
Are there any concerns with using a pointer as described above, or with attempting to do so without the intermediate pointer?
Would using memcpy be considered preferable under certain scenarios?
The ultimate use of this is processing a Big Endian byte stream, so I'm using byteorder(3)
functions as appropriate.