Pointer & Double pointer, C language pointer
Pointer is a difficulty in C language, and it is inevitable in C ++.
The following are some of the shortcomings of my learning experience.
Pointer type:
Pointer
Const int * pstr cannot modify the object to which the pointer is directed. It can point the pointer to another object.
For example, const int * pvalue {& value };
* Pvalue = 6; // will not compile
Pvalue = nullptr; // OK
Int * const pstr cannot modify the address stored in the pointer. You can modify the object to which the pointer belongs.
For example, Int * const pvalue {& value };
* Pvalue = 6; // OK
Pvalue = nullptr; // will not compile
The Const int * const Pstr pointer and the object to which it points cannot be modified.
For example, Const int * const pvalue {& value };
* Pvalue = 6; // will not compile
Pvalue = nullptr; // will not compile
Pointer array char * pstr []
Array pointer char (* pstr) [4]
Double beans [3] [4];
Double * pbeans;
Pbeans = & beans [0] [0]; // point to the position of the first element of the array
Pbeans = & beads [0]; // point to the position of the element in the first row of the array
Double (* pbeans) [4] = beans; // non = & beans, with the same number of Columns
Function pointer
Double (* pfun) (char *, int)
Pointer parameters:
First, let's look at the address output of a double pointer (second-level pointer) in the mian function.
1 int main() 2 { 3 int v = 9; 4 int *p1 = &v; 5 int **p2 = &p1; 6 7 cout << "v= " << dec << v << endl; 8 cout << "&v= " << hex << &v << endl; 9 cout << "p1= " << hex << p1 << endl;10 cout << "&p1= " << hex << &p1 << endl;11 cout << "&*p1=" << hex << &(*p1) << endl;12 13 cout << "p2= " << hex << p2 << endl;14 cout << "&p2= " << hex << &p2 << endl;15 cout << "&(*p2)=" << hex << &(*p2) << endl;16 17 cin.get();18 return 0;19 }
Note: It is not difficult to see the relationship between pointers. The second-level pointer p2 points to p1, and the physical memory p2 stores the address of the pointer p1, the physical memory location of p1 stores the V address of the variable.
When the pointer is passed as a parameter in the C language, you can use the address to observe the function call situation.
1 void SetPoint(int *pu) 2 { 3 cout << "pu= " << hex << pu << endl; 4 cout << "&pu= " << hex << &pu << endl; 5 *pu=*pu + 1; 6 } 7 int main() 8 { 9 int a = 5;10 int *pt = &a;11 SetPoint(&a);12 13 cout << "&a= " << hex << &a << endl;14 cout << "pt= " << hex << pt << endl;15 cout << "&pt= " << hex << &pt<<endl;16 cout << "&*pt=" << hex << &(*pt)<<endl;17 cout << "a= " << dec << a<< endl;18 19 cin.get();20 return 0;21 }
Note: The code above shows that the address of the 'put' parameter is not equal to the address of the variable 'A', so 'put' is only used as a temporary variable to point to the variable 'A '.
The value of 'A' is changed, indicating that the compiler has created a temporary variable to operate on 'A.
1 void SetPoint(int &pu) 2 { 3 cout << "pu= " << hex << pu << endl; 4 cout << "&pu= " << hex << &pu << endl; 5 pu=pu + 1; 6 } 7 int main() 8 { 9 int a = 5;10 int *pt = &a;11 SetPoint(a);12 13 cout << "&a= " << hex << &a << endl;14 cout << "&pt= " << hex << &pt<<endl;15 cout << "&*pt=" << hex << &(*pt)<<endl;16 cout << "a= " << dec << a<< endl;17 18 cin.get();19 return 0;20 }
Note: The reference in c ++ directly operates on the variable 'A'. From the above output, we can see that the 'put' address is equal to the 'A' address.
Double pointer:
The following is a frequently asked question on the Internet:
Void GetMemory (char * p, int num) {p = (char *) malloc (sizeof (char) * num);} int main () {char * str = nullptr; getMemory (str, 100); strcpy (str, "Hello"); cout <str <endl; return 0 ;}
I tried to output the str and p addresses, and the result is on. We can see that the addresses of the two variables are not the same, so p is used as a temporary variable in function running. We can see that, like directly passing a parameter, it will only be treated as a temporary variable.
You can pass the temporary variable in the function as the return value to str to achieve the desired effect.
char * GetMemory(char *p, int num){ p = (char *)malloc(sizeof(char) * num); return p;}int main(){ char *str = nullptr; str=GetMemory(str, 100); strcpy(str, "Hello"); cout << str << endl; return 0;}
The double pointer (second-level pointer) is used directly for high efficiency)
void GetMemory(char **p, int num){ *p = (char *)malloc(sizeof(char) * num); }int main(){ char *str = nullptr; GetMemory(&str, 100); strcpy_s(str,6, "Hello"); cout << str << endl; cin.get(); return 0;}
Think about the output address. The pointer p [00 AFFDDC] points to the address of str [00AFFEB4. In this way, the second-level pointer is used as a bridge curve operation to directly allocate space for str.
Therefore, you can use dual pointers to apply for dynamic memory allocation in the future. In the face of big data, it is a waste of time to copy and move data.