Nar*_*esh 0 c pointers pointer-to-pointer
我已将整数变量的地址存储在指针中,然后将该先前的地址存储到另一个指针中.我无法理解它是如何工作的.
#include <iostream>
using namespace std;
#include <stdio.h>
int main ()
{
int var;
int *ptr;
int **pptr;
var = 30;
/* take the address of var */
ptr = &var;
/* take the address of ptr using address of operator & */
pptr = &ptr;
/* take the value using pptr */
printf("Value of var = %d\n", var );
printf("Value available at ptr = %d\n", ptr );
printf("Value available at pptr = %d\n", pptr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当你这样做时,&ptr你会得到变量的地址ptr.
所以你有一个指针指向pptr哪个指针ptr依次指向var.像这样:
+------+ +-----+ +-----+ | pptr | --> | ptr | --> | var | +------+ +-----+ +-----+
另外,请不要使用"%d"格式来打印指针.请"%p"改用,然后将指针强制转换为void *.