C + + The problem of accepting the value of an array with a pointer variable as a function parameter detailed summary _c language

Source: Internet
Author: User
/tr>
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

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

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.