- オンラインコンパイラpaiza
#include <iostream>
using namespace std;
#include <string.h> //memcpy
int main(void){
// Your code here!
unsigned char buf[4] = {0x12,0x34,0x43,0x21};
for(int i = 0;i<4;i++){
printf("[%02x]",buf[i]);
}//for
printf("\n");
struct {
short x;
short y;
} X_Y;
memcpy( &X_Y, &buf, ( sizeof(buf) < sizeof(X_Y) ) ? sizeof(buf) : sizeof(X_Y) );
printf("X_Y.x=%x\n",X_Y.x);
printf("X_Y.y=%x\n",X_Y.y);
} //main
[12][34][43][21]
X_Y.x=3412
X_Y.y=2143