fork download
  1. // For a discussion see http://stackoverflow.com/q/23697560/228539
  2.  
  3. #include <stdio.h>
  4. #include <stdint.h>
  5. #include <string.h>
  6.  
  7. #define BUFFER_SIZE 10
  8. #define ADDRESS 6
  9.  
  10. uint8_t buffer[BUFFER_SIZE] =
  11. {
  12. [ADDRESS] = 0x37,
  13. [ADDRESS+1] = 0x42,
  14. };
  15.  
  16. int main(void)
  17. {
  18. uint16_t a = buffer[ADDRESS] + (buffer[ADDRESS + 1]<<8);
  19. uint16_t b = *(buffer + ADDRESS) + (*(buffer + ADDRESS + 1)<<8);
  20. uint16_t c = *((uint16_t *) (buffer + ADDRESS));
  21. uint16_t d;
  22. memcpy(&d, &buffer[ADDRESS], sizeof(d));
  23.  
  24. printf("a: 0x%x\n", a);
  25. printf("b: 0x%x\n", b);
  26. printf("c: 0x%x\n", c);
  27. printf("d: 0x%x\n", d);
  28.  
  29. return 0;
  30. }
  31.  
Success #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
a: 0x4237
b: 0x4237
c: 0x4237
d: 0x4237