The pointer stores the memory address, so when the code is executed
int *iptr;int a;iptr = &a;
It indicates that iptr points to the memory address of. If
*iptr = 10;printf("a = %d\n",a);
So what is the value of?
You can simply write a program cpoint1.c
#include<stdio.h>int main(){int a;int *iptr;iptr = &a;*iptr = 10;printf("a = %d\n",a);}
Compile with GCC on the terminal
#gcc cpoint1.c
Generate a. out execution file after execution
./a.out
The printed result is a = 10.
The pointer iptr points to the memory address of.
*iptr = 10;
Actually, 10 is saved to the memory address of.
Now that we understand the basic concepts of pointers, what are pointers?
First, let's assume that James, Xiaohua, and xiaoxin are friends, but James only has Xiaohua's phone number, while Xiaohua only has xiaoxin's phone number. If Tom wants to find Tom, he must first contact Tom and then contact Tom through Tom.
If Xiao Hua and Xiao Xin are both treated as memory addresses, Then Xiao Ming points to Xiao Hua, which can be understood as a level-1 pointer. Then Xiao Hua points to Xiao Xin, which becomes a pointer to the pointer.
int *iptr;int a;int **jptr;
We already know above.
iptr = &a;
It refers to assigning the memory address of a to iptr.
Then how should we assign a value to the jptr pointer. The example of James Xiaohua xiaoxin just now can be dissected in this way.
Int ** Xiao Ming; int * Xiao Hua; int Xiao Xin;
First, James needs to get Xiao Xin's address, so he needs to use Xiao Hua. So there are
Xiao Hua = & Xiao Xin;
In this way, Xiaohua points to xiaoxin, obtains the address of xiaoxin, and transfers the address of Xiaohua to Xiaoming, so that Xiao Ming can get the address of xiaoxin.
Xiao Ming = & Xiao Hua;
Next, let's look at the following program cpoint2.c.
#include<stdio.h>int main(){int a;int *iptr;int **jptr;iptr = &a;jptr = &iptr;a = 10;printf("jptr = %d\n",**jptr);}
#gcc cpoint2.c
The result of compilation and execution is:
jptr = 10
Do you understand?