Four ways of combining real and formal parameters
actual Parameters |
Formal Parameters |
instance |
array name |
|
|
array name |
pointer variable |
1.2 |
pointer variable |
|
|
pointer variable |
pointer variable |
1.4 | /tr>
This article takes 10 integers, then sorts them, and then prints out the program as an example:
The parameter is an array name and the argument is a name
Instance Code 1.1:
Copy Code code as follows:
#include <iostream>
using namespace Std;
int main () {
void Sort (int a[],int n);
int a[10],i;
cout<< "Please input 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;
}
}
A[] In formal parameters, you can write no numbers, just indicate that this is an array. If you put a number in it, you can place any positive integer (not necessarily equal to the size of the real argument group, or larger or smaller than the array in the argument).
That
Copy Code code as follows:
void Sort (int a[], int n)
can also be written
Copy Code code as follows:
void Sort (int a[2], int n)
Or
Copy Code code as follows:
void Sort (int a[12], int n)
The argument is an array name, and the formal parameter is a pointer variable
Instance Code 1.2:
Copy Code code as follows:
#include <iostream>
using namespace Std;
int main () {
void Sort (int a[],int n);
int a[10],i;
cout<< "Please input 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 "C + + one-dimensional array and pointer relationship summary", it has been mentioned that an array name actually represents the address of the first element of an array, which means that a is equivalent to &a[0]
In the argument, the array name represents the address of the first element in the array, so the argument actually passes only the pointer to the first element of the array. Therefore, in the formal parameter, only one pointer variable is required to accept the passed value.
The argument is a pointer variable, and the formal parameter is an array
Instance Code 1.3:
Copy Code code as follows:
#include <iostream>
using namespace Std;
int main () {
void Sort (int a[],int n);
int a[10],i;
cout<< "Please input 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;
}
}
Consistent with the above analysis, the &a[0] is used as the argument to pass the value, proving that the array name actually represents the address of the first element of the array, which means that a is equivalent to &a[0]
The formal parameter is a pointer variable, and the argument is a pointer variable
Instance Code 1.4:
Copy Code code as follows:
#include <iostream>
using namespace Std;
int main () {
void Sort (int a[],int n);
int a[10],i;
cout<< "Please input 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 method is the most straightforward method, and the values passed by the arguments and the parameters and the values accepted are pointers