In depth parsing of C++ pointer arrays and Pointers to Pointers
- 2020-04-02 01:25:00
- OfStack
Pointer to an array
Definition:
If the elements of an array are all pointer data, the array is a pointer array, that is, each element in the pointer array is equivalent to a pointer variable, and its value is an address.
Form:
The one-dimensional pointer array is defined as:
Int [type name] *p [array name] [4] [array length];
Since [] has a higher priority than *, p is first combined with [4] to form an array of p[4]. Then combined with the "*" before p, "*" means that the array is pointer type, each array element is equivalent to a pointer variable, can point to the plastic variable.
Note: it cannot be written as an int (*p)[4], which is a pointer to a one-dimensional array.
Each element in the pointer array is used to point to several strings, which makes the processing of strings more flexible.
Program 1.1
#include<iostream>
using namespace std;
int main(){
void sort(char *p[],int n);
void print(char *p[],int n);
char *name[]={"C","C++","PHP","ASP","ASP.NET","C#","JAVA","BASIC","PASCAL","COBOL"};
int n=10;
sort(name,n);
print(name,n);
return 0;
}
void sort(char *p[],int n){
char *temp;
int i,j,k;
for(i=0;i<n;i++){
k=i;
for(j=i;j<n;j++){
if(strcmp(p[j],p[k])<0){
k=j;
}
}
if(k!=i){
temp=p[k];
p[k]=p[i];
p[i]=temp;
}
}
}
void print(char *p[],int n){
int i;
for(i=0;i<n;i++){
cout<<p[i]<<endl;
}
}
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/2013090210261624.jpg ">
Analysis:
The main function defines the pointer array name, whose ten elements are strings
"C","C++","PHP","ASP","ASP.NET","C#","JAVA","BASIC","PASCAL","COBOL"
The starting address of. Then pass the address of the first element of the array to the p array in the sort, so that the parameter p and argument name point to the same array. The array is then sorted by selection.
Print function is to output each string, p[0]~p[9] is the first address of each string.
The print function can also be rewritten as follows:
void print(char *p[],int n){
char *q=p[0];
int i=0;
while(i<n){
q=*(p+i++);
cout<<q<<endl;
}
}
Pointer to a pointer
Definition:
A pointer to pointer data is a pointer to a pointer, such as the pointer array name[10] defined in the main function in program 1.1.
char * *p=name
Is the address of the first pointer element of the pointer array to the pointer to the variable p;
Program 1.2
#include<iostream>
using namespace std;
int main(){
char *name[]={"C","C++","PHP","ASP","ASP.NET","C#","JAVA","BASIC","PASCAL","COBOL"};
char * *p;
p=name+2;
cout<<p<<endl;
cout<<*p<<endl;//Equivalent to the name [2]
cout<<* *p<<endl;
return 0;
}
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/2013090210261725.jpg ">
Analysis:
P is the pointer to the pointer, which is the value of the address of the name[2].
*p is the pointer, which is the value of name[2] (the element in the pointer array);
* *p is the value of the data that the pointer points to, because the p defined is the data that points to type char, so the result outputs the first character.