I am wondering why, when I copy a 16-bit number to a double-byte array, it only results in copying to the first index of the array. My code is as follows:
#include <iostream>
#include <stdint.h>
#include <stdio.h>
#include <cstring>
using namespace std;
int main(){
uint16_t my_num = 1;
unsigned char my_arr[2];
memcpy(my_arr, &my_num, sizeof(my_num));
printf("%x ", my_arr[0]);
printf("%x ", my_arr[1]);
cout << endl;
return 0;
}
Thanks in advance.
source
share