编写一个函数,该函数将返回i
x
个b
字节已替换为unsigned replace_f (unsigned x, int i, unsigned char b){
}
的值:
replace_f(0x12345678, 2, 0xBC) --> 0x12BC5678
EX:{{1}}
答案 0 :(得分:1)
没试过,但这可行:
unsigned replace_f (unsigned x, int i, unsigned char b){
char *c;
c = (char *)&x;
c[i] = b;
return x;
}
答案 1 :(得分:1)
unsigned replace_f (unsigned x, int i, unsigned char b){
unsigned char *place = (unsigned char*)&x;
place[sizeof(int)-i] = b;
return x;
}
假设小端