C pointer parsing ------ arithmetic operations on pointers
This article is my learning notes, welcome to reprint, but please note the Source: http://blog.csdn.net/jesson20121020
A pointer is a special variable that represents an address, and the address can be subtracted from or added with an integer to represent a new address.
The following code analyzes the arithmetic operations of pointers:
# Include
Int main () {int I; char str [] = {'A', 'B', 'C', 'D', 'E '}; int * p = str; char * q = str; q ++; p ++; printf ("str's first address: % x \ n", str ); printf ("internal zone pointed by pointer q: % x \ n", q); printf ("memory zone pointed by pointer p: % x \ n", p ); printf ("value of the first element of the array: % c \ n", * str); printf ("value of the first element of the array: % c \ n ", * p); // The first method to traverse the array. The subscript method printf ("the first method to traverse the array, the subscript method:"); for (I = 0; I <sizeof (str); I ++) printf ("% c", str [I]); // traverses the array. Method 2, array name pointer method printf ("\ n traversal array method 2, array name pointer method:"); for (I = 0; I <sizeof (str); I ++) printf ("% c", * (str + I); // traverses the third method of the array, the pointer method q = str; printf ("\ n traverses the third method of the array, pointer method: "); for (I = 0; I <sizeof (str); I ++) printf (" % c ", * q ++ );}
Execution result:
We can see that the first address of the array is 163c4730. through the pointer concept and four elements in the previous section, it is easy to know that the q type of the pointer is char *, and the q type of the pointer is char; char * q = str; the function is to assign the first address of the array str to the pointer q, that is, the value of the pointer q or the memory zone pointed by q is the first address of the array str. next, the pointer q is automatically added to 1. In this case, the compiler processes the pointer q with sizeof (char ), that is, a byte is moved backward based on the original address, that is, the memory zone pointed to by the pointer q is 163c4730 + 1 = 163c4731. similarly, the pointer p type is int * and the pointer type is int, int * p = str; the function is to assign the first address of the array to the pointer p, p ++, the compiler adds the sizeof (int) value of the pointer p, that is, four bytes are moved backward based on the original one, that is, the memory zone pointed by the pointer p is 163c4734, therefore, the pointer p points to the four bytes starting with unit 0th before the auto-increment, and points The four bytes starting from unit 4 of the array.
Therefore, when judging how many bits the pointer moves backward, You need to determine the type to which the Pointer Points.
You can also use pointers to traverse the array. For example, you can use the array name as the array name points to the cell 0th of the array. If an offset is added to the array, you can access other elements of the array. Therefore, you can use the array name + offset to traverse the array. Similarly, another pointer points to the cell 0th of the array. Each time the pointer is incremented by 1, the entire array can be traversed. Note that the type pointed to by the pointer, to be consistent with the element type of the array, the above Code uses the pointer q to traverse the array, instead of using the pointer p to traverse.
Of course, if the pointer is subtracted from an integer, the processing process is similar to the previous process, but only moves forward rather than backward.
To sum up, after a pointer ptrold is added with an integer n, the result is a new pointer ptrnew. The type of ptrnew is the same as that of ptrold, ptrnew points to the same type as ptrold. The value of ptrnew will be increased by n multiplied by sizeof (type pointed to by ptrold) bytes than the value of ptrold. That is to say, ptrnew will point to a memory area that moves n byte by sizeof (type pointed by ptrold) to the high address direction than the memory area pointed to by ptrold. After a pointer ptrold is subtracted from an integer n, the result is a new pointer ptrnew. The type of ptrnew is the same as that of ptrold, and the type of ptrnew is the same as that of ptrold. The value of ptrnew will be less than the value of ptrold by n multiplied by sizeof (type pointed to by ptrold) bytes, that is, the memory area to which ptrnew points will move n times of sizeof (type pointed by ptrold) bytes to the lower address direction than the memory area to which ptrold points.