The essence of a pointer is the address, the variable of the pointer is the variable that holds the address, and the pointer has a pointer to a level two pointer.
I. Introduction to first-level pointers
1. The pointer variable is defined in the following format:
Data type * pointer variable name
int *p; (P is the pointer variable name, type int)
Char *ch; (ch is the pointer variable name, type char)
......
What you need to know is that pointer variables can only be used to store addresses;
* Indicates that this variable is a pointer variable;
* pointer variable name, which indicates the contents of the memory space that the pointer variable points to;
int means that this pointer variable can only hold the address of a variable of type int, and the pointer variable also has global variables and local variables.
2. Initialization and reference of pointer variables
The initialization of pointer variables can be divided into 4 types, the following is the initialization and reference of pointer variables.
1) Define the same time and initialize
int *p=&a; (int type, the pointer is called P-N is the address, the address of A is assigned to P);
It can be seen that the address of a is the same as the value of P, indicating that the address of a is given the pointer variable p. Because P is a pointer variable, which is a variable, it can be re-assigned if it is a variable. That
The essence is that the direction of the P pointer has changed.
2) Define the pointer variable first, and then initialize the
int *p1;
*p1=&b; (defined before B);
3) can be initialized with a pointer variable that already exists.
4) Assign a value of NULL to the pointer variable;
where int *p5=null indicates that the pointer variable P5 points to a place in memory where address is 0.
From the above, * There are two occasions to use: the first is that when defining a variable, a pointer variable is defined, and the second is a pointer variable name that gets the contents of the memory space that the pointer variable points to.
Summarize the knowledge of the first-level pointers:
int num=10;
int *p=num;
(P is a pointer variable, p has the address of &p,p is the address of NUM is &num)
Two. Introduction of Level Two pointers
The above introduction is a first-level pointer, the next introduction is the definition and use of level two pointers. What is a level two pointer? A secondary pointer is the address used to hold the pointer variable!
The definition of a first-level pointer:
Data type * pointer variable name
int *p; (P is the pointer variable name, type int)
Char *ch; (ch is the pointer variable name, type char)
The definition of a second-level pointer:
Data type * * pointer variable name
int **p; (P is a two-level pointer variable name, type int)
Also, if printf ("p1=%p\n", p1) means take P1 address!
Moreover, the level two pointer can only hold the address of the first-level pointer variable, if you direct the level two pointer directly to the address of the constant is not possible! As follows:
From the definition above that is wrong, be careful not to commit!
Of course there are three levels of pointers, level three pointer is the address of the two-level pointer variable! int ***p3=&p2;
Summary of a pointer (definition of a pointer)