修改指针在C ++函数中指向的位置 [英] Modify the location a pointer points to in a C++ function
问题描述
我已经坚持修改指针的指针。问题是,我不明白为什么我的代码工作。我想做的是修改指针指向指针在一个函数中的位置。然后在我的主函数中访问该值。我尝试了相当多的尝试,这是我使它工作的唯一方式。
I have gotten stuck on modifying pointers of pointers. The problem is that I don't understand why my code works. What I am trying to do is modify where a pointer-to-pointer points to in a function. Then accessing that value in my main function. I tried quite a few attempts and this is the only way I got it to work.
#include <iostream>
using namespace std;
void changePP(int **ppint) {
int *n = new int;
*n = 9; //just a value for demonstration purposes
*ppint = n; //THE LINE IN QUESTION
delete n;
}
int main() {
int **ppint = NULL;
int *p = new int;
*p = 4; //another value for demonstrating
ppint = &p;
cout << **ppint << endl;
changePP(ppint);
cout << **ppint << endl;
}
4,然后在单独的行上9。但是,我不知道代码中的行 * ppint = n
为什么我必须使用 *
来更改ppint在changePP函数中指向的位置,而不是在main中?为什么我不必在函数中使用&
?我不能找到一个我可以在互联网上理解的解释,我想知道有人能为我解释这个。
So, the output is 4 and then a 9 on seperate lines. However, I am not sure about the line *ppint = n
in the code. Why do I have to use the *
to change where ppint points to in the changePP function but not in the main? Also why do I not have to use the &
in the function? I can't seem to find an explanation that I can understand on the internet and I was wondering if someone could explain this for me?
推荐答案
注意:这些内存地址仅供参考。
Note: these memory addresses are only for illustration.
0x0A | 4
0x0B | 'p' -> 0x0A // Pointer to the value 4 in location 0x0A
0x0C | 'ppint' -> NULL // Initially a null pointer
执行 ppint =& p; / code>产生以下结果,因为
ppint
是位于 0x0C
的指针。
0x0A | 4
0x0B | 'p' -> 0x0A
0x0C | 'ppint' -> 0x0B
对于 changePP
函数,我将其称为 ppintCopy
,以减少混乱。它是一个副本(即,不同于 ppint
的指针),并修改它只修改副本。
As for the parameter in the changePP
function, I will refer to it as ppintCopy
for less confusion. It's a copy (i.e., a pointer different than ppint
) and modifying it only modifies the copy.
0x0D | 'ppintCopy' -> 0x0C // A pointer that points to `ppint`
执行 ppintCopy =& ; n
将修改 0x0D
处的指针,而不是 main
函数。 但,引用 ppintCopy
(即 * ppintCopy
)会产生 0X0C
,它是 main
函数的指针,因此 * ppintCopy = n;
其中指针 0x0C
指向。
Performing ppintCopy = &n
would modify the pointer at 0x0D
which is not the location of the pointer from the main
function. However, deferencing ppintCopy
(i.e., *ppintCopy
) yields 0X0C
which is the pointer from the main
function therefore *ppintCopy = n;
is changing where the pointer 0x0C
points.
为什么我必须使用*改变哪里ppint指向changePP函数,但不在主?
Why do I have to use the * to change where ppint points to in the changePP function but not in the main?
在上面的插图中我希望它更清楚为什么在 main
中工作,以及为什么必须在 changePP
函数中使用不同的语法。
With the above illustration I hope it is more clear why it works in main
and why you have to use a different syntax in the changePP
function.
为什么我不必使用&在函数中
Also why do I not have to use the & in the function?
在 main
函数中,变量 ppint
的类型为 int **
(即指向指针的指针)和 p
的类型为 int *
。您不能执行 ppint = p;
,因为它们是不同的类型。如果删除一个级别的间接,这可能更容易看到。例如:
In the main
function the variable ppint
is of type int**
(i.e., a pointer to a pointer) and p
is of type int*
. You cannot perform ppint = p;
because the they are different types. This may be easier to see if you remove one level of indirection. For example:
int* pMyInt = NULL;
int myInt = 3;
我认为这是很自我解释,这不能工作(即,他们是不同的类型) p>
I think it's pretty self explanatory that this cannot work (i.e., they are different types).
pMyInt = myInt;
However you can take the address of myInt
using the &
operator which results in a pointer to int (i.e., int*
) and since this type is the same as the type of pMyInt
the assignment is now possible.
pMyInt = &myInt;
这篇关于修改指针在C ++函数中指向的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!