C指针管理(地址、解引用、重新分配)
我正在尝试编写一个函数,它将访问一个字节并将其更改为另一个字节。我管理指针有困难 有人能帮我修复这段代码,让px指向地址,然后我可以增加pc(与px相同,但与char一样,以避免增加太多字节),然后用其他东西替换mem位置pc上的内容吗C指针管理(地址、解引用、重新分配),c,pointers,byte,bits,C,Pointers,Byte,Bits,我正在尝试编写一个函数,它将访问一个字节并将其更改为另一个字节。我管理指针有困难 有人能帮我修复这段代码,让px指向地址,然后我可以增加pc(与px相同,但与char一样,以避免增加太多字节),然后用其他东西替换mem位置pc上的内容吗 unsigned replace_byte(unsigned x, int i, unsigned char b){ unsigned int* px = &x; //Takes initial address of first byte.
unsigned replace_byte(unsigned x, int i, unsigned char b){
unsigned int* px = &x; //Takes initial address of first byte.
unsigned char* pc= (unsigned char*)px; //Recast it too char
//pointer to avoid incrementing by too many bytes
if (i==0) {
*pc= b; //if i is 0 then replace the byte at mem location pc
//with b
}
if (i==1) { //if i is 1 or more then inc by that much and replace
pc = pc+1;
*pc= b;
}
if (i==2) {
pc=pc+2;
*pc= b;
}
if (i==3) {
pc=pc+3;
*pc= b;
}
return x;
}
我得到的返回值如下:
305419947
305441656
313218680
2872333944
当我想要得到这样的值时:
替换字节(0x12345678,0xAB,2)-->0x12AB5678
替换字节(0x12345678,0xab,0)-->0x123456AB这对我来说非常有效:
#include <stdio.h>
unsigned replace_byte(unsigned x, int i, unsigned char b){
unsigned int px = x;
unsigned char* pc= (unsigned char*) &px;
if (i > 3) return px;
*(pc + i) = b;
return px;
}
int main()
{
int a = 0x12345678;
a = replace_byte(a, 2, 0x7F); // any hexadecimal character in parameter 3
printf("%x", a);
}
#包括
无符号替换_字节(无符号x,整数i,无符号字符b){
无符号整数px=x;
无符号字符*pc=(无符号字符*)&px;
如果(i>3)返回px;
*(pc+i)=b;
返回px;
}
int main()
{
INTA=0x12345678;
a=替换_字节(a,2,0x7F);//参数3中的任何十六进制字符
printf(“%x”,a);
}
此代码演示如何更改int类型的特定字节
#include <stdio.h>
int main() {
unsigned int i = 0xffffffff;
//// make two pointers to the one memory address
// x - pointer to int. The address will be shifting by 4 bytes, when this
// pointer will be shifting by 1.
int *x = &i;
// y - pointer to char. The address will be shifting by 1 byte, when this
// pointer will be shifting by 1.
char *y = (char *) x;
//addresses the same here.
printf("x address = %p\n", x);
printf("y address = %p\n", y);
puts("");
//both pointers are shifting by 1, but addresses are different now.
printf("x + 1 = %p\n", x+1);
printf("y + 1 = %p\n", y+1);
puts("");
//changing the 'i' original value by one byte in turns, one after another.
printf("i - original: %x\n", i);
*y = 16;
printf("i - the first byte changed: %x\n", i);
*(y+1) = 16;
printf("i - the second byte changed: %x\n", i);
*(y+2) = 16;
printf("i - the third byte changed: %x\n", i);
*(y+3) = 16;
printf("i - the forth byte changed: %x\n", i);
return 0;
}
什么东西不起作用(除了你应该写
return x
而不是return printf…
)?尊重我们的时间正确地写代码这到底是什么:return printf(“%s\n”;)代码>根据您的编辑,整数以4个字节存储,每个字节重1,2,4,8,16,。。。。。。替换一个字节将极大地改变数字。如果希望输出看起来像替换字节(0x12345678,0xAB,2)-->0x12AB5678,则需要分别写入每个字节。@Jas因此,如果我获取地址,然后递增,我可以分别访问每个字节,并在其内存位置编辑值?对吗?这就是我想做的,但显然我做错了。@Ozzoned当然,你可以单独访问每个字节,并根据你的意愿编辑它,你这样做确实是正确的。正在修改字节。问题是你的输出不是你所期望的,对吗?这是因为计算机不会将整数解释为4个单独的字节,而是将它们解释为单个大数字。在某个地方多读一些,你的疯狂数字是正确的。整数的第一个字节从0到255,下一个字节更重,从255到32767。所以,如果你把第二个字节增加一,变化将是巨大的。我相信我确实想要这个地址。我的思想过程是获取内存中第一个字节的地址,然后从那里递增以访问其他字节。一旦我有了地址,我就可以返回到该值并更改该地址的值。
x address = 0x7fffe59e9794
y address = 0x7fffe59e9794
x + 1 = 0x7fffe59e9798
y + 1 = 0x7fffe59e9795
i - original: ffffffff
i - the first byte changed: ffffff10
i - the second byte changed: ffff1010
i - the third byte changed: ff101010
i - the forth byte changed: 10101010