Pointers can point to an ordinary type of data, such as int, double, char, or to a pointer type of data, such as int *, double *, char *, and so on.
If a pointer is pointing to another pointer, we call it a level two pointer, or a pointer to a pointer.
Suppose a variable a,p1 of type int is a pointer variable to a, P2 is a pointer variable that points to a P1, and their relationship is shown in the following illustration:
Convert this relationship to C language code:
int a =100;
int *P1 = &a;
int **P2 = &p1;
A pointer variable is also a variable that consumes storage space and can use & to get its address. C language does not limit the series of pointers, each additional level of pointers, in the definition of pointer variables will have to add an asterisk *. P1 is a pointer to the normal type of data, defined with a *;P2 is a level two pointer, pointing to the first pointer P1, defined with two *.
If we want to define a three-level pointer P3 and point it to P2, you can write this:
int ***P3 = &p2;
The four-level pointer is similar:
int ****P4 = &p3;
In actual development, first-level pointers and level two pointers are frequently used, with almost no advanced pointers.
To get the data that the pointer points to, a level pointer plus a *, a level two pointer plus two *, a three-level pointer plus three *, and so on, look at the code:
#include <stdio.h>
int main () {
int a =100;
int *P1 = &a;
int **p2 = &p1;
int ***p3 = &p2;
printf ("%d,%d,%d,%d\n", A, *p1, **P2, ***p3);
printf ("&P2 =% #X, p3 =% #X \ n", &P2, p3);
printf ("&P1 =% #X, p2 =% #X, *p3 =% #X \ n", &p1, p2, *p3);
printf ("&a =% #X, p1 =% #X, *p2 =% #X, **p3 =% #X \ n", &a, p1, *p2, **p3);
return 0;
}
Run Result:
100, 100, 100, 100.
&P2 = 0x28ff3c, p3 = 0x28ff3c
&P1 = 0x28ff40, p2 = 0x28ff40, *p3 = 0x28ff40
&a = 0x28ff44, p1 = 0x28ff44, *p2 = 0x28ff44, **p3 = 0x28ff44
Take the three-level pointer P3 as an example to analyze the above code. P3 is equivalent to * (* (*P3)). *p3 Gets the P2 value, which is the P1 address; * (*P3) Gets the value of the P1, which is the address of a, after three "value" operations, * (* (p3)) is the value of a.
Suppose the addresses of a, p1, p2, p3 are 0x00a0, 0X1000, 0X2000, 0x3000, and the relationship between them can be described in the following illustration:
Inside the box is the value of the variable itself, and the box below is the address of the variable.
Above is the C language two levels of data collation, follow-up continue to supplement the relevant information, thank you for your support for this site!