The pointer can add and subtract integers. The meaning of this operation of pointer is different from the usual numerical addition and subtraction operation, and it is in units. For example:
Example 2:
char a[20];
int * ptr =(int *)a; //The cast will not change.
ptr++;
In the above example, the type of pointer ptr is int*, and the type it points to is int, which is initialized to point to integer variable A. In the next third sentence, pointer ptr is added with 1, which is how the compiler handles it: it adds sizeof(int) to the value of pointer ptr, and in 32-bit programs, it is added with 4. Because the address is in bytes, the address pointed by ptr is increased by 4 bytes from the original address of variable A to the high address direction. Because the length of char type is one byte, the original ptr refers to four bytes starting from element 0 of array A, and now it points to four bytes starting from element 4 of array A ... We can use pointers and loops to traverse the array. See example:
Example 3:
int array[20]= { 0 };
int * ptr = array
for(I = 0; I & lt20; i++)
{
(* ptr)++;
ptr++;
}
This example adds 1 to the value of each cell in an integer array. Because the pointer ptr adds 1 cells in each loop, the next cell of the array can be accessed in each loop.
Look at this example again:
Example 4:
Char a[20]= "You are a girl";
int * ptr =(int *)a;
ptr+= 5;
In this example, ptr is added with 5, and the compiler handles it like this: add 5 to the value of pointer ptr and multiply it by sizeof(int), which is 5 times 4=20 in a 32-bit program. Because the unit of address is bytes, the address pointed by the current ptr is 20 bytes higher than that pointed by ptr plus 5. In this example, the ptr before adding 5 points to the first four bytes of cell 0 of array A, and after adding 5, ptr has pointed to array A..
Outside the scope of the law. Although this situation will cause problems in application, it is possible in grammar. This also shows the flexibility of the pointer. If in the above example, ptr is reduced by 5, then the process is similar, except that the value of ptr is reduced by 5 times sizeof(int), and the address pointed by the new ptr will be moved 20 bytes lower than that pointed by the original ptr.
Let me give you another example: (a misunderstanding)
Example 5:
# Including
int main()
{
Char a[20]= "You are a girl";
char * p = a;
char * * ptr = & ampp;
//printf("p=%d\n ",p);
//printf("ptr=%d\n ",ptr);
//printf("*ptr=%d\n ",* ptr);
printf(" * * ptr = % c \ n " ,* * ptr);
ptr++;
//printf("ptr=%d\n ",ptr);
//printf("*ptr=%d\n ",* ptr);
printf(" * * ptr = % c \ n " ,* * ptr);
}
Misunderstanding 1. The output answers are y and o.
Misunderstanding: ptr is the secondary pointer of char, when executing ptr++; When, it will add a sizeof(char) to the pointer, so the output of the above result may only be the result of a few people.
Myth 2. The output answers are y and a.
Misunderstanding: ptr refers to a char * type, when executing ptr++; It will add a sizeof(char *) to the pointer (some people may think that this value is 1, and then they will get the wrong answer of 1, which should be 4, refer to the previous content), that is&; p+4; Won't the value operation point to the fifth element in the array? Isn't the output the fifth element in the array? The answer is no. Positive solution: the type of ptr is char **, and the type it points to is char *, so it should point to.
The address is the address of P (&; P), when ptr++ is executed; , will increase the pointer by a sizeof(char*), namely &; p+4; That * (&; P+4) Where does it point? Ask God, or he will tell you where it is. So the final output will be a random value, perhaps an illegal operation.
To sum up:
After adding (subtracting) an integer n to the pointer ptrold, the result is a new pointer ptrnew, whose type is the same as that of ptrold, and the type pointed by ptrnew is also the same as that pointed by ptrold. The value of ptrnew will be increased (decreased) by n times of sizeof (the type that ptrold points to) bytes than sizeof(ptrold). That is to say, the memory area pointed by ptrnew will move N times of sizeof (ptrold type pointed by ptrold) bytes in the high (low) address direction than the memory area pointed by PTR old.
Pointer and addition and subtraction pointer:
Two pointers cannot be added, which is illegal, because the result after adding points to an unknown place and is meaningless. Two pointers can be subtracted, but they must be of the same type, which is usually used in arrays, so I won't say much.
Second, the address operator &; And indirect operator *
Here & is the address operator and * is the indirect operator. The operation result of & ampA is a pointer, the type of pointer is the type of A plus a *, the type of pointer is the type of A, and the address of pointer is the address of A. *p has various operation results. In short, the result of *p is pointed by p, which has these characteristics: its type is the type pointed by p, and the address it occupies is the address pointed by p.
Example 6:
int a = 12; int b; int * p; int * * ptr
p = & ampa; //& amp; The result of is a pointer of type int*, pointing to the type.
//int, pointing to a.
* p = 24//*p, where its type is int and the address it occupies is
//p points to the address. Obviously, *p is the variable A..
ptr = & ampp; //& amp; The result of p is a pointer whose type is p plus a *.
//This is int **. The pointer points to the type of P.
//is int*. The address pointed by the pointer is the address of the pointer p itself.
* ptr = & ampb; //*ptr is a pointer, and the result of&b is also a pointer, and these two pointers
The type of//is & as the pointing type, so *ptr is allocated with & ampb.
//The value is ok.
* * PTR = 34//* The result of ptr is what ptr points to, and here is the pointer.
//Do another * operation on this pointer, and the result is a variable of type int.