C ++ pointer array and pointer to pointer

Source: Internet
Author: User

Pointer Array

 

Definition:
If an array contains all pointer-type 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 definition of a one-dimensional pointer array is as follows:

Int [type name] * p [array name] [4] [array length ];

Because [] is higher than *, p is first combined with [4] to form an array of p [4. Then it is combined with "*" before p. "*" indicates that the array is of the pointer type. Each array element is equivalent to a pointer variable and can point to an integer variable.

Note: it cannot be written as int (* p) [4]. This refers to a pointer variable pointing to a one-dimensional array.


Use each element in the pointer array to point to several strings respectively to make string processing 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;     } } #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; }}

 
 

Analysis:
The main function defines the pointer array name. Its ten elements are strings.

 

"C","C++","PHP","ASP","ASP.NET","C#","JAVA","BASIC","PASCAL","COBOL" "C","C++","PHP","ASP","ASP.NET","C#","JAVA","BASIC","PASCAL","COBOL"

. Then, the address of the first element of the array is uploaded to the p array in the sort function. Therefore, the form parameter p and the real parameter name point to the same array. Then, the array is sorted by the selection method.

The print function outputs 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;     } } void print(char *p[],int n){    char *q=p[0];    int i=0;    while(i<n){     q=*(p+i++);     cout<<q<<endl;    }}


Pointer to pointer

 

Definition:

The pointer to the pointer data is the pointer to the pointer, for example, the pointer array name [10] defined in main function of program 1.1.

 

 

Char ** p = name indicates that the address of the first pointer element in the pointer array is assigned to the variable p pointing to the pointer;

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 name [2] cout <** p <endl; return 0 ;} # 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 name [2] cout <** p <endl; return 0 ;}

#


Analysis:


P is the pointer to the pointer, that is, the value of the address of the stored name [2;

* P is the pointer, that is, the value of name [2] (element in the pointer array );

** P indicates the data value pointed to by the pointer. Because the defined p indicates data of the char type, the first character is output.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.