我想知道为什么当我将一个16位数字复制到一个两个字节的数组中时,结果只复制到了数组的第一个索引。
我的代码如下:
#include <iostream>
#include <stdint.h>
#include <stdio.h>
#include <cstring>
using namespace std;
int main(){
uint16_t my_num = 1; // This should be 0000 0000 0000 0001, right?
unsigned char my_arr[2]; // This should hold 16 bits, right?
memcpy(my_arr, &my_num, sizeof(my_num)); // This should make my_arr = {00000000, 00000001}, right?
printf("%x ", my_arr[0]);
printf("%x ", my_arr[1]);
cout << endl;
// "1 0" is printed out
return 0;
}
Thanks in advance.
0x01
,然后是0x00
。 - Frédéric Hamidi00000001 000000000
。 - Shark