将指针地址分配给指针到指针

时间:2019-07-15 09:04:05

标签: c pointers pointer-to-pointer

我正在尝试使用指针来编写简单的C代码,但是内存被意外覆盖。

我想创建一个指向整数的指针数组,然后创建一个指向整数的指针,并将其地址分配给数组。

因此,数组将指向一个指向整数的指针。我是在名为add_value_to_array()的函数中完成此操作的。

这是我的代码:

void add_value_to_array(int *** array, int * value) {
    *array = &value;
}
int main() {

    int ** arr = { 0 };
    int value = 5;
    int * value_ptr =  &value;

    add_value_to_array(&arr, value_ptr);
    //arr = &value_ptr;
    printf("looool\n");
    printf("looool\n");
    printf("looool\n");
    printf("looool\n");
    printf("%p\n", arr[0]);

}

我想要的是:

arr -> value_ptr -> 5
(arr = &value_ptr
*array = value_ptr
value_ptr = &value
*value_ptr = 5
**arr = 5)

但是,这是在调用add_value_to_array()之后我所拥有的,但是当我调用printfs()时内存被覆盖了,我在arr变量中得到了垃圾值。

即使很奇怪,如果我直接执行arr = &value_ptr而不是调用add_value_to_array,事情也会按预期进行,printfs()不会覆盖内存。

因此,如果我使用某个函数或在该函数之外执行其他操作,则似乎内存分配有所不同。

我看到此行为是怎么回事?

2 个答案:

答案 0 :(得分:2)

以下假设:您要创建一个长度为1(可能更长的长度)的数组,该数组指向int。并且您希望数组中的单个指针指向名为“值”的局部变量。

然后:

int* arr[] = { 0 }; // or { NULL }, if you prefer
//       ^  creates an array; either you specify size explicitly or it will be deduced
//          from initializer, as in this case

如果将数组传递给函数,则数组会自动衰减为指向第一个元素的指针。所以:

add_value_to_array(arr, &value);
//                 ^ decays to pointer
//                        ^ here you still need a pointer; you can create it directly, though

剩下的问题很少:arr的指针类型为int**。您的函数中需要相同的类型:

void add_value_to_array(int** array, int* value)
//                          ^ one asterisk less
{
    *array
//  ^ this one is NOW correct: array points to the first element of arr
//    dereferencing gives you exactly this element, i. e. the pointer

        = /* & */value;
//           ^ must be dropped: you already have a pointer and you assign it to
//             another one, which is fine.
}

请注意,指针只是内存中某处变量的地址。有点简化:

int n = 10;
int* p0 = &n;
int* p1 = &n; // p1 and p0 point both to the same variable n
int* p2 = p0; // now again p2 points to n
*p0 = 12;     // change the value pointed to
printf("%d\n", *p2); // will print 12, as both pointers point to the same address.

函数参数在这方面没有区别,它们只是普通变量。如果指针是数据值或指针本身,它将不起作用:

int n = 10;
int m = 12;
int* p = &n;
int** p1 = &p; // pointer to pointer to int
int** p2 = p1; // both p1 and p2 point to p
*p1 = &m;      // re-assign the POINTER
printf("%d\n", **p2); // again prints 12:
                      // p1 and p2 both point to p which was reset to m, though...

答案 1 :(得分:0)

谢谢大家的回答,这有助于我发现错误: 我必须按地址而不是按值传递value_ptr。
这是我的更正代码:

void add_value_to_array(int *** array, int ** value_ptr) {
    *array = value_ptr;
}
int main() {

    int ** arr = { 0 };
    int value = 5;
    int * value_ptr =  &value;

    add_value_to_array(&arr, &value_ptr);
    //arr = &value_ptr;
    printf("looool\n");
    printf("looool\n");
    printf("looool\n");
    printf("looool\n");
    printf("%p\n", arr[0]);

}

谢谢大家的帮助!

相关问题