【问题标题】:C Pointer Mangement (Address, Dereferencing, Reassigning)C 指针管理(地址、取消引用、重新分配)
【发布时间】:2017-09-07 20:54:57
【问题描述】:

我正在尝试编写一个函数来访问一个字节并将其更改为另一个。我在管理指针时遇到了麻烦。

有人可以帮我修复这段代码,让 px 指向地址,然后我可以增加 pc (这与 px 相同,但就像 char 以避免增加太多字节),然后替换 mem 中的内容用其他东西定位电脑?

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

当我想得到这样的值时:

replace_byte(0x12345678, 0xAB, 2) --> 0x12AB5678 replace_byte(0x12345678, 0xab, 0) --> 0x123456AB

【问题讨论】:

  • 究竟是什么不工作(除了你应该写return x而不是return printf...)?
  • 尊重我们的时间正确编写代码这是什么鬼:return printf("%s\n", ););
  • 根据您完全有意义的编辑,整数以 4 个字节存储,每个位重 1,2,4,8,16,...... 替换一个字节将彻底改变数字。如果您希望输出看起来像 replace_byte(0x12345678, 0xAB, 2) --> 0x12AB5678,那么您需要分别写入每个字节。
  • @Jas 那么如果我获取地址然后递增,我可以分别访问每个字节并在其内存位置编辑值?对?这就是我想要做的,但显然我做错了什么......
  • @Ozzoned 当然,您可以单独访问每个字节并根据需要对其进行编辑,并且您的操作方式确实是正确的。字节正在被修改。问题是您的输出不是您期望的正确?这是因为计算机不会将整数解释为 4 个 单独 字节,而是将它们解释为 单个 大数。在某处阅读更多信息,您的疯狂数字是正确的。整数的第一个字节从 0 到 255,下一个更重,从 255 到 32,767。因此,如果您将第二个字节增加 ONE,则更改将是巨大的。

标签: c pointers byte bits


【解决方案1】:

这对我来说非常有效:

#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);
}

【讨论】:

  • 我相信我确实想要这个地址。我的想法是获取内存中第一个字节的地址,然后从那里递增以访问其他字节。获得地址后,我可以返回该值并更改该地址的值。
【解决方案2】:

此代码演示如何更改 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;
}

输出:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多