Initialization assignment increment
. 1 #include <bits / STDC ++ H.> 2 the using namespace STD; . 3 . 4 int main () . 5 { . 6 int A = . 3 , * P1, * P2; . 7 P1 = & A ; . 8 P2 = P1; // same Types assignment . 9 the printf ( " a = D%, D% = P1, D P2 =% \ n- " , a, P1 *, * P2); 10 . 11 * P1 = 10 ; // is actually a change of address contains changing a value of indirect value 12 is the printf ( " a = D%, D% = P1, D P2 =% \ n- " , a, P1 *, *P2); 13 is 14 * P2 = 33 is ; // actual value change is a change indirect address contains a value of 15 the printf ( " a = D%, D% = P1, D P2 =% \ n- " , a, P1 *, * P2); 16 . 17 a = 89 ; // direct itself to change a value 18 is the printf ( " a = D%, D% = P1, D P2 =% \ n- " , a, P1 *, * P2 ); . 19 20 is * * P1 = P1 + . 1 ; // increment 21 is the printf ( " A = D%, D% = P1, D P2 =% \ n- " , A, P1 *, * P2); 22 is 23 is * ++ P1; 24 the printf ( " A = D%, D% = P1, D P2 =% \ n- " , A, P1 *, * P2); 25 26 is (P1 *) ++ ; 27 the printf ( " A D% =,% = D p1, D P2 =% \ n- " , a, * p1, * P2); 28 29 // the following is the address plus one, a byte is increased, so they no longer point p1 a a, * p1 also changed 30 * P1 ++ ; 31 is the printf ( " a = D%, D% = P1, D P2 =% \ n- " , a, * p1, * P2); 32 33 is - * P1 ; 34 is the printf ( " A = D%, D% = P1, D P2 =% \ n-" , A, P1 *, * P2); 35 // A and * p2 does not change 36 37 [ - * P2; 38 is the printf ( " A = D%, D% = P1, D P2 =% \ n- " , A, P1 *, * P2); 39 return 0 ; 40 }
Classic examples pointer
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 void swap1(int x,int y) 5 { 6 int t; 7 t = x; 8 x = y; 9 y = t; 10 } 11 12 void swap2(int *px,int *py) 13 { 14 int t; 15 t = *px; 16 *px = *py; 17 *py = t; 18 } 19 20 void swap3(int *px,int *py) 21 { 22 int *t; 23 t = px; 24 px = py; 25 py = t; 26 } 27 28 int main() 29 { 30 int a = 1,b = 2; 31 int *pa = &a,*pb = &b; 32 swap1(a,b); 33 cout << a << " " << b << " " << *pa << " " << *pb << endl; 34 35 a = 1,b = 2; 36 swap2(pa,pb); 37 cout << a << " " << b << " " << *pa << " " << *pb << endl; 38 39 a = 1, b = 2; 40 swap3(pa,pb); 41 cout << a << " " << b << " " << *pa << " " << *pb << endl; 42 43 return 0; 44 }
The relationship between pointers, arrays, address
When defining the array, the compiler must be assigned base address and sufficient storage space to store all the elements of the array.
The start of the array base address stored in the memory array, the array also the address of the first element.
Arrays and pointers
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define rep(i,x,y) for(int i = x;i <= y;i++) 4 int main() 5 { 6 int a[100],*p,sum = 0; 7 8 rep(i,0,99) 9 a[i] = i + 1; 10 11 p = a; 12 //p = &a[0];等价于上面 13 cout << *p << endl; 14 15 p = a + . 1 ; 16 // P = A & [. 1]; equivalent to the above . 17 COUT * P << << endl; 18 is // first sum . 19 SUM = 0 ; 20 is for (P = A; P <= A & [ 99 ]; P ++ ) 21 is SUM + * = P; 22 is COUT SUM << << endl; 23 is 24 // second summing 25 SUM = 0 ; 26 is for ( int I = 0 ; I < 100 ; I ++ ) 27 SUM + = * (A + I); 28 COUT SUM << << endl; 29 30 // third summing 31 is P = A; 32 SUM = 0 ; 33 is for ( int I = 0 ; I < 100 ; I ++ ) 34 is SUM + = P [I] ; 35 COUT SUM << << endl; 36 // name of the array may be in the form of a pointer 37 [ return 0 ; 38 is }
There is the essential difference
A pointer array is constant, not variable a = p, a ++, a + = 2; is illegal and can not change the value of a
Calculating the number of memory cells and the array element number of the array elements using pointers.
Pointer operation is an important feature of the c
If the pointer points to a particular type of variable variable p, then p + 1 represented by the following expression a variable memory address p + i, p ++, p + = i of this type; is meaningful
If p and q points to the array element pointer, p - q generate an int value that indicates the number of array elements between p and q. Similarly pointer plus or minus not a pointer value of +1 or -1
But which variable plus or minus the length of the pointer data type, i.e., it refers to the storage unit by the number of bytes occupied.
If p points to an array a [1] pointer, int type, a [1] addresses 2004, p-- value is 2002
. 1 #include <stdio.h> 2 . 3 int main () . 4 { . 5 Double A [ 2 ], P *, * Q; . 6 P = A & [ 0 ]; . 7 Q = P + . 1 ; . 8 the printf ( " % D \ n- " , QP); // count the number of elements between p, q . 9 // the printf ("% D \ n-", (int) q - (int) p); // calculate the pointer p and q the number of bytes between 10 return 0 ; . 11 }
Array name as a function parameter
Shaped array is actually a pointer to a parameter. When the transfer parameters, the main function is passed the array a base address of the array itself is not replicated.
The compiler allows the use of square brackets in the array as a pointer in the parameter declaration
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 int sum(int a[],int n) 5 { 6 int s = 0; 7 for(int i = 0;i < n;i++) 8 s += a[i]; 9 return s; 10 } 11 int main() 12 { 13 int a[100]; 14 for(int i = 0;i < 100;i++) 15 a[i] = i + 1; 16 cout << sum(a,100) << endl; 17 return 0; 18 }