[英]Assigning address of pointer to pointer-to-pointer
I am trying to make a simple piece of c code using pointers work but memory is being overwritten unexpectedly. 我正在尝试使用指针来制作简单的C代码,但是内存被意外覆盖。
I want to create an array of pointers to integers then create a pointer to an integer and assign it's address to an array. 我想创建一个指向整数的指针数组,然后创建一个指向整数的指针,并将其地址分配给数组。
Therefore, the array will point to a pointer to an integer. 因此,数组将指向一个指向整数的指针。 I did this in a function called
add_value_to_array()
. 我是在名为
add_value_to_array()
的函数中完成此操作的。
Here is my code : 这是我的代码:
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]);
}
What I want is : 我想要的是:
arr -> value_ptr -> 5
(arr = &value_ptr
*array = value_ptr
value_ptr = &value
*value_ptr = 5
**arr = 5)
however, this is what I have right after add_value_to_array()
is called, but memory is overwritten when I call the printfs()
, I get garbage values in my arr
variable. 但是,这是在
add_value_to_array()
之后所add_value_to_array()
,但是当我调用printfs()
时,内存被覆盖了,我的arr
变量中得到了垃圾值。
Even weirder, if I do directly arr = &value_ptr
instead of calling add_value_to_array
, things go as expected and memory is not overwritten by the printfs()
. 甚至很奇怪,如果我直接执行
arr = &value_ptr
而不是调用add_value_to_array
,事情也会按预期进行,并且内存不会被printfs()
覆盖。
So it seems that memory is allocated differently if I use a function or if I do I do things outside of it. 因此,如果我使用一个函数或执行其他操作,则似乎内存分配有所不同。
What is happening that I am seeing this behavior? 我看到此行为是怎么回事?
Following assumption: You want to create an array of length 1 (maybe greater length later) of pointers to int. 遵循以下假设:您想要创建一个长度为1(可能更长的长度)的数组,该数组指向int。 And you want that single pointer in the array to point to the local variable named 'value'.
并且您希望数组中的单个指针指向名为“值”的局部变量。
Then: 然后:
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
Arrays automatically decay to pointer to first element, if you pass them to a function. 如果将数组传递给函数,则数组会自动衰减为指向第一个元素的指针。 So:
所以:
add_value_to_array(arr, &value);
// ^ decays to pointer
// ^ here you still need a pointer; you can create it directly, though
Little problem left: arr
decaying to pointer is of type int**
. 剩下的小问题:
arr
到指针的arr
类型为int**
。 You need the same type in your function: 您的函数中需要相同的类型:
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.
}
Be aware that pointers simply are addresses of variables somewhere in memory. 请注意,指针只是内存中某处变量的地址。 A bit simplified:
有点简化:
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.
Function parameters do not differ in this respect, they are just ordinary variables. 函数参数在这方面没有区别,它们只是普通变量。 And it doesn't play a role if the pointee is a data value or a pointer itself:
如果指针是数据值或指针本身,它将不起作用:
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...
Thank you all for your answers, this helped me find the bug : I had to pass my value_ptr by address and not by value. 谢谢大家的回答,这帮助我找到了错误:我必须按地址而不是按值传递value_ptr。
Here is my corrected code : 这是我的更正代码:
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]);
}
Thank you all for your help ! 谢谢大家的帮助 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.