Speaking * and + + are used for pointers at the same time raised the question : What to dereference, what increment.
Double arr[5]={21.132.823.445.237.4 }; double *pt=arr; // pt pointer pointing to arr[0] value is 21.1++pt; // pt pointer pointing to arr[1] value is 32.8
The right-to-left binding rule for the prefix operator means that
The meaning of *++p is as follows:
Apply + + to PT first (because + + is on the right of *) and then increment the * to the incremented pt-- pointer
Double // Pointer +1, point to arr[2] value is 23.4
The meaning of ++*pt is as follows:
means to get the value PT points to, and then increment the value by one--- point
// point to the value +1 23.4+1=24.4
(*pt) + + has the following meanings:
Parentheses indicate that the pointer is de-referenced first, resulting in 24.4. Then the operator + + increments the value to 25.4,pt still points to arr[2]
*p++ has the following meanings:
x=* pt++;
The postfix operator + + has a higher precedence, which means that the operator is used for PT instead of *PT, so the pointer is incremented.
The suffix operator, however, means that the original address (&arr[2]) is de-referenced instead of the incremented new address, so the value of *pt++ is arr[2], or 25.4,
However, the value of PT will be the address of arr[3] When the statement is executed.
#include"stdafx.h"#include<iostream>intMain () {using namespacestd; Doublearr[5]={21.1,32.8,23.4,45.2,37.4}; Double*pt=arr; ++pt; intA; Doublex; cout<<*++pt<<Endl; cout<<++*pt<<Endl; cout<< (*PT) ++<<Endl; cout<<*pt++<<Endl; X=*pt++; cout<<x<<Endl; CIN>>A;}
Note: The pointer increment and decrement follow the pointer's arithmetic rules, and if PT points to the first element of an array,++PT modifies the PT value (the first element)and points to the second element address (the second element value is unchanged)
"Original" learn C + + pointers--/++---------C + + Primer Plus (6th edition)