Introduction: an important C language data type is a pointer, the pointer is one of the characteristics of the C language.
Proper flexible use of the pointer, you can make programming simple, compact and efficient.
The pointer variables can effectively represent complex data structures, such as a queue (Queue), the stack (Stack), the list (Linked Table), a tree (Tree), FIG (Graph), etc., is due to review the data structure, discovered that the C language pointer learn is not good, this part is really a bit difficult pointer, study recently spent a few days pointer, summed up.
Therefore, the master and the proper use of language refers to C programmers for a success is very important.
Topic:
The most important thing is to learn pointers to remember two things: ① pointer = address, address = pointer address pointer is equivalent ② drawing, drawing, drawing (drawing can be well understood, or else going around, and for a while fainted)
(1) First, in the computer, all data is stored in the internal memory in binary form (referred to as memory) of.
It is to draw the memory map.
first name | Memory address | content |
For example: a | For example: 0x6000 | Storage of data, variables, |
p1 | 0x6001 | |
.... | 0x6002 | |
0x6003 | ||
0x6004 | ||
0x6005 | ||
0x6006 | ||
0x6007 | ||
…… | …… |
Access content access via the row address label needs to be divided into direct access (addressing) and indirect access (address), a pointer corresponding to indirect access (address).
Direct addressing: a storage unit can be operated directly by its address variable
Indirect access (addressing): is a variable a, which kept the contents of the address on the contents of a another address, this variable is called p, this can be considered a pointer p. Drawing it directly, to see more clearly.
first name | Memory address | Content (data storage, variables) |
For example: a | For example: 0x6000 | A variable content |
0x6001 | ||
0x6003 | ||
0x6004 | ||
p (pointer variable) | 0x6005 | 0x6000 (the content of the address pointer variable p = pointer) |
0x6006 | ||
0x6007 | ||
…… | …… |
(2) define the start pointer variables (important to understand better use * asterisk )
① general form of a pointer variable definitions:
[] Data type modifiers type * variable name list;
E.g:
* p int; // * asterisk is a separate variable name p, p is a pointer variable, not * p is a pointer variable.
// this * asterisk is very important to indicate with back pointer variable, the variable is not an ordinary
② pointer variable initialization
int * p = & a; // this is known as the star * pointer or the operator called "indirect access memory address" operator;
// When defining, through it indicates that a variable is defined as a pointer variable,
// while in use, * p represents the content of variable p points. It is to take the contents, drawing, drawing, drawing follows
p (pointer variable) | 0x6005 | 0x6000 (the content of the address pointer variable p = pointer) |
* P, p content taken as 0x6000, which is the last one taken
For example: a | For example: 0x6000 | A variable content |
& A fetch address a, is taken that the intermediate column.
This address into the p in as the content, this address is the pointer.
Finally, there is a number of related terms, pointers, pointer variable, pointer array, an array of pointers, function pointers, function pointers, pointers to pointers ,, depends on what is the last term, to which end of the term,
For example an array of pointers, the last term is an array, it is described an array and interpreted as an array of pointers to the (respective elements are pointers) char * P [10] ;
Array pointer array name itself is a pointer, the first address to an array, note that this is a constant. char (* P) [10];
Being so much.