简体   繁体   English

如何修复 C 代码中的指针问题?

[英]How can I fix pointer problem in my C code?

#include <stdio.h>
void test(void **arg) {
    int foo = 3;
    **(int **)arg = foo; // I want to just fix this line!!
}

int main(void) {
    int temp = 0;
    printf("%d\n", temp);

    test((void **)&temp);

    printf("%d\n", temp);

    return 0;
}

In my code, It has problem 'Segmentation fault', but I don't know how can I fix my code..在我的代码中,它有问题“分段错误”,但我不知道如何修复我的代码..

I wanna fix just **(int **)arg = foo;我只想修复**(int **)arg = foo; line.线。

Can Anybody Help Me?有谁能够帮我?

In your code, function test(void **temp) , variable temp is a pointer to a pointer, aka double pointer.在您的代码中, function test(void **temp) ,变量 temp 是指向指针的指针,也就是双指针。 That is to say, it value is an address.也就是说,它的值是一个地址。 But when you call test from main, that value is 0, which means that address is address 0.但是当你从 main 调用 test 时,那个值是 0,这意味着地址是地址 0。

You can not assign a value to address 0.您不能为地址 0 分配值。

It looks like you are writing to address 0.看起来您正在写入地址 0。

Because:因为:

&temp is a pointer to int. &temp是一个指向 int 的指针。

*((int**)&temp) is an int. *((int**)&temp)是一个整数。

**((int**)&temp) use your value from temp as address. **((int**)&temp)使用来自 temp 的值作为地址。

Your function你的 function

void test(void **arg);

expects a "pointer to pointer to void" , that is the address of a location that contains another address pointing to a generic data.需要一个“指向 void 的指针” ,即包含另一个指向通用数据的地址的位置的地址。

When you call the function you are not passing what it expects当您调用 function 时,您没有通过它的预期

int temp = 0;
test((void **)&temp);

In fact &temp is an address pointing to an integer.事实上&temp是一个指向 integer 的地址。 But it expects the address of an address, So, when inside the function you deference it twice (every deferencing with the * operators means "resolving" one address) the second time you are trying to access the address 0.但是它需要一个地址的地址,因此,当在 function 内部时,您在第二次尝试访问地址 0 时将其延迟两次(使用*运算符的每次延迟都意味着“解析”一个地址)。

In order to fix it, just pass to test a pointer to pointer:为了修复它,只需通过test指向指针的指针:

int temp = 0;
int *tempAddr = &temp; //tempAddr points to temp

test((void **)&tempAddr); //The type of the address of tempAddr is 'int  **'

You were actually asking a different thing: you explicitly asked for a fix of the **((int **) arg) = foo;您实际上是在问另一件事:您明确要求修复**((int **) arg) = foo; line.线。

That's not so easy, as you are currently receiving an invalid pointer to pointer, and there's no way to make it valid just changing that line.这并不容易,因为您当前收到了一个无效的指针指针,并且仅更改该行就无法使其有效。 In order to sort it out you would need to change test() interface, like shown below:为了解决它,您需要更改test()接口,如下所示:

#include <stdio.h>
void test(void *arg) { // changed 'void **' to 'void *'
    int foo = 3;
    *(int *)arg = foo; // only one level of dereferencing
}

int main(void) {
    int temp = 0;
    printf("%d\n", temp);

    test((void *)&temp); // cast to 'void *' instead of 'void **'

    printf("%d\n", temp);

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM