如何在c中编写替换函数

时间:2012-09-13 05:00:22

标签: c replace

编写一个函数,该函数将返回i xb字节已替换为unsigned replace_f (unsigned x, int i, unsigned char b){ } 的值:

replace_f(0x12345678, 2, 0xBC) --> 0x12BC5678

EX:{{1}}

2 个答案:

答案 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;
}

假设小端