-4

编写一个函数,该函数将返回一个值,其中i-th 字节x已被 替换b

unsigned replace_f (unsigned x, int i, unsigned char b){

}

前任:replace_f(0x12345678, 2, 0xBC) --> 0x12BC5678

4

2 回答 2

2

没试过,但这可以工作:

unsigned replace_f (unsigned x, int i, unsigned char b){
    char *c;

    c = (char *)&x;
    c[i] = b;
    return x;
}
于 2012-09-13T05:17:50.853 回答
2
unsigned replace_f (unsigned x, int i, unsigned char b){
    unsigned char *place = (unsigned char*)&x;
    place[sizeof(int)-i] = b;
    return x;
}

假设小端

于 2012-09-13T05:23:10.053 回答