gpt4 book ai didi

c - 将指针的地址分配给指针到指针

转载 作者:太空宇宙 更新时间:2023-11-04 02:21:39 26 4
gpt4 key购买 nike

我正在尝试使用指针编写一段简单的 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().

因此,如果我使用一个函数,或者如果我在函数之外做一些事情,内存分配似乎是不同的。

我看到这种行为是怎么回事?

最佳答案

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

然后:

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...

关于c - 将指针的地址分配给指针到指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57036444/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com