C++ USES pointer variables as function parameters to accept array values

  • 2020-04-02 01:57:27
  • OfStack

Four ways of combining arguments and parameters The arguments parameter The instance An array of An array of 1.1 An array of Pointer to the variable 1.2 Pointer to the variable An array of 1.3 Pointer to the variable Pointer to the variable 1.4

This article takes an example of a program that inputs 10 integers, sorts them, and outputs them:

The parameter is the array name and the argument is the array name

Example code 1.1:


#include<iostream>
using namespace std;
int main(){
 void Sort(int a[],int n);
 int a[10],i;
 cout<<"Please input 10 interges: "<<endl;
 for(i=0;i<10;i++){
  cin>>a[i];
 }
 Sort(a,10);
 cout<<"Sorted order:";
 for(i=0;i<10;i++){
  cout<<a[i]<<" ";
 }
 cout<<endl;
 return 0;
} 
void Sort(int a[], int n){
 int i,j,k,tool;
 for(i=0;i<n;i++){
  k=i;
  for(j=i;j<n;j++){
   if(a[j]<a[k])
   k=j; 
  }
  tool=a[k];
  a[k]=a[i];
  a[i]=tool;
 }
}

In a[], you can not write any Numbers, just to indicate that this is an array. If you put a number in it, you can place any positive integer (not necessarily the size of the real argument group, but larger or smaller than the array in the argument).

That is:


void Sort(int a[], int n )

Or you could write it as

void Sort(int a[2], int n)

or

void Sort(int a[12], int n)

The argument is the name of the array and the parameter is the pointer variable
Example code 1.2:

#include<iostream>
using namespace std;
int main(){
 void Sort(int a[],int n);
 int a[10],i;
 cout<<"Please input 10 interges: "<<endl;
 for(i=0;i<10;i++){
  cin>>a[i];
 }
 Sort(a,10);
 cout<<"Sorted order:";
 for(i=0;i<10;i++){
  cout<<a[i]<<" ";
 }
 cout<<endl;
 return 0;
} 
void Sort(int *a, int n){
 int i,j,k,tool;
 for(i=0;i<n;i++){
  k=i;
  for(j=i;j<n;j++){
   if(a[j]<a[k])
   k=j; 
  }
  tool=a[k];
  a[k]=a[i];
  a[i]=tool;
 }
}

In the article summary of the relationship between C++ one-dimensional arrays and Pointers, it was mentioned that the array name actually represents the address of the first element of the array, which means that a is equivalent to &a[0].

In arguments, the array name represents the address of the first element in the array, so the argument actually just passes a pointer to the first element of the array. Therefore, in the parameter, only one pointer variable is needed to accept the passed value.

Arguments are pointer variables and parameters are arrays

Example code 1.3:


#include<iostream>
using namespace std;
int main(){
 void Sort(int a[],int n);
 int a[10],i;
 cout<<"Please input 10 interges: "<<endl;
 for(i=0;i<10;i++){
  cin>>a[i];
 }
 Sort(&a[0],10);
 cout<<"Sorted order:";
 for(i=0;i<10;i++){
  cout<<a[i]<<" ";
 }
 cout<<endl;
 return 0;
} 
void Sort(int a[], int n){
 int i,j,k,tool;
 for(i=0;i<n;i++){
  k=i;
  for(j=i;j<n;j++){
   if(a[j]<a[k])
   k=j; 
  }
  tool=a[k];
  a[k]=a[i];
  a[i]=tool;
 }
}

In line with the previous analysis, the array name is passed as an argument by &a[0], which proves that the array name actually represents the address of the first element of the array, that is, a is equivalent to &a[0].

The parameter is a pointer variable and the argument is a pointer variable

Example code 1.4:


#include<iostream>
using namespace std;
int main(){
 void Sort(int a[],int n);
 int a[10],i;
 cout<<"Please input 10 interges: "<<endl;
 for(i=0;i<10;i++){
  cin>>a[i];
 }
 Sort(&a[0],10);
 cout<<"Sorted order:";
 for(i=0;i<10;i++){
  cout<<a[i]<<" ";
 }
 cout<<endl;
 return 0;
} 
void Sort(int *a, int n){
 int i,j,k,tool;
 for(i=0;i<n;i++){
  k=i;
  for(j=i;j<n;j++){
   if(a[j]<a[k])
   k=j; 
  }
  tool=a[k];
  a[k]=a[i];
  a[i]=tool;
 }
}

This is the most straightforward method, the passed and accepted values of arguments and parameters are Pointers


Related articles: