C language Learning for a long time, for which the pointer is not very clear understanding, take advantage of the opportunity to study, summarize the knowledge learned, knowledge from the C language pointer detailed article
One: the concept of pointers
A pointer is a special variable that stores a value that is an address in memory. To learn pointers, it is important to figure out the four aspects of the pointer: the type of the pointer, the type the pointer points to, the memory area that the pointer points to, and the memory area that the pointer itself occupies.
1, how to determine the type of pointer:
int *p;char *p;int **p;int (*p) [3];int * (*P) [4];
The above five declarations, the types of pointers are: int *, char *, int * *, int (*) [3], int * (*) [4], is it very simple, is to remove the variable name, the rest is the type of pointer.
2. The type that the pointer points to
int *p;char *p;int **p;int (*p) [3];int * (*P) [4];
In the above five declarations, the type pointed to by the pointer is: Int\char \int *\int () [3]\int * () [4], find out the law? is to remove the variable name and a *, and the rest is the type that the pointer points to. In particular, the type of pointer and the type that the pointer points to are different concepts.
3. The value of the memory area or pointer to which the pointer is pointing
The value of the pointer is the value stored by the pointer itself, which is treated as an address by the compiler instead of a generic value. In a 32-bit program, all types of pointers have a 32-bit value, because the memory addresses in 32-bit programs are all 32-bit word lengths.
The memory area that the pointer points to is the memory area represented by the value of the pointer, and the length is a memory area of sizeof (the type the pointer is pointing to). Later, we say that the value of a pointer is XX, which means that the pointer points to an area of memory with the first address of XX; we say that a pointer points to a memory area, and the value of the pointer is the initial address of the memory area.
The pointer to whom it is pointing, assigns the address to the pointer.
4. The memory area occupied by the pointer itself
How much memory the pointer itself occupies. Use sizeof (the type of pointer) to test one and know it. In a 32-bit platform, the pointer itself occupies a length of four bytes.
Second, the arithmetic operation of the pointer
Char A[20];int *p=a;p++;
The type of the pointer p is int *, pointer to the type is int, it is initialized bit to shape variable A, then the pointer p is added 1, the compiler does this: it adds the value of the pointer p to sizeof (int), in a 32-bit program, is added 4. Since the address is in bytes, the address of P is pointed to the address of the original a, which points to the high address after adding four bytes.
Since the length of the char type is one byte, the original p is the four bytes that point to the array starting at 0, and now points to four bytes starting from the fourth byte
We can iterate over an array with a pointer and a loop:
int Arr[20];int *p=arr;.......//The code for the integer assignment is omitted here for (i = 0; i<20; i++) { (*p) + +; p++; }
This example adds 1 to the value of each cell in an integer array, since each loop will have the pointer p, so each loop can access the next cell of the array.
Char A[20];int *p=a;......p+=5;
In this example, P is added in 5, from the compiler's point of view: The value of the pointer p plus sizeof (int), in the 32-bit Heng Xu is added 5*4=20. Because of the unit Cross street of the address, p now points to the first 20 bytes in the high-byte direction. In this example, p before 5 points to the four bytes starting at the No. 0 cell of the array, and after adding 5, p already points out of the legal range of the array. Although this situation may be problematic in application, it is syntactically possible. This demonstrates the flexibility of the pointer.
Summing up: After a pointer, plus an integer n, the result is a new pointer to the type of pnew,pnew and the type of pnew pointed to is the same as the type of pold. The value of pnew increases the byte of n*sizeof (the type that p points to) than the value of Pold. Plus is toward high byte movement, minus is toward low byte movement.
Three Operators & and *
The & is the address operator, which is called the indirect operator in the book. The result of the &a operation is a pointer that is of type A and a *. The type that the pointer points to is the type of a, and the address that the pointer points to is the address of a. The result of the *p is the result that P points to, the type is the type of p, and the address it occupies is the address that P points to.
The result of an int a =12;int b;int *p;int *ptr;p=&a//&a is a pointer, the type of the pointer is an int *, the pointer points to a data type of int, and the address to a is the address of a. *p=24;//*p The result, its type is int, it occupies the address is P point to the address, obviously *p is the variable a*ptr=&p;//p itself is a pointer, p in the address, then the type of the pointer is int * *, the pointer is the next type of int *. The address that the pointer points to is the address of P **ptr=34;//*ptr The result of what PTR is pointing to. Here is a pointer to this pointer in doing a * operation,. The result is a variable of type int
Four-pointer expression
The final result of an expression if it is a pointer, the expression is called a pointer expression
int a, b; int array[10];int *pa;pa=&a;//pointer expression int **ptr = &pa;//expression *ptr=&b;//expression pa=array;pa++;//expression char arr[ 20];char **parr =arr//If you think of arr as a pointer, arr is also a pointer expression char *str;str=*parr;//pointer expression str=* (parr+1);//Pointer expression
Because the result of a pointer expression is a pointer, the pointer expression also needs to have four features of the pointer: the type of the pointer, the type that the pointer points to, the memory area that the pointer is pointing to, and the memory area that the pointer occupies.
The pointer represents an assignment on the left, and the pointer to the right represents a value.
Not to be continued
C Language Pointer Learning