A collection of questions about array function parameters _c language

Source: Internet
Author: User
Tags numeric value

The first is that the array element acts as an argument to the function, and there is no difference in usage between the arguments that are used directly with multiple variables as functions.

Code as an example:

Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
int max (int a,int b);
int a[2],m;
A[0]=1;
a[1]=2;
M=max (a[0],a[1]);
cout<<m;
return 0;
}
int max (int a,int b) {
if (a<b) a=b;
return A;
}

The output result is: 2

Then, using the array name as the function argument, the array name actually represents the pointer to the first element of the array.

Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
void alter (int b[]);//b[] The value in parentheses can be written without writing, and its function is to enable the compilation system to treat it as a one-dimensional array
int a[2];
A[0]=1;
a[1]=2;
Alter (a);
cout<<a[0]<< "\ n";
cout<<a[1]<< "\ n";
return 0;
}
void alter (int b[]) {
b[0]=3;
b[1]=4;
}

The output results are:

3

4

If we write this:

Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
void alter (int b[20]);//b[] The value in parentheses can be written without writing, and its function is to enable the compilation system to treat it as a one-dimensional array
int a[2];
A[0]=1;
a[1]=2;
Alter (a);
Cout<<sizeof (a);
return 0;
}
void alter (int b[20]) {
Cout<<sizeof (b) <<endl;
}

Output results:

4

8

Why do we have defined a[2] and also assign value, after passing to function, the size is only one unit?

In fact, we define b[] or b[2] or b[1], b[20, b[100], which acts as *b. The compiler simply ignores the values in parentheses.

For a higher description, the array masterpiece is actually the first pointer of an array passed by the argument, and you can look at this example again:

Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
void alter (int *b);
int a[2];
A[0]=1;
a[1]=2;
Alter (a);
cout<<a[0]<< "\ n";
cout<<a[1]<< "\ n";
return 0;
}
void alter (int *b) {
*b=3;
* (b+1) = 4;
}

This is exactly the same output as the one above!

================================ Split Line ==========================

Next, to sum up, the reference to the array is related to the problem

The first is an example of a generic variable as a reference to a value in an array:

Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
int a[2];
A[0]=1;
a[1]=2;
int &t=a[0];
t=5;
cout<<a[0]<< "\ n";
return 0;
}

The output result is: 5

When a generic variable is used as a reference to a value in an array, there is no difference between using and int &a=b;.

It is natural for us to think that a normal variable can be used as a reference to an array element, so could an array element be used as a reference to another element?

Look at the following code:

Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
int a[2];
int b=100;
&a[0]=b;//is not allowed to do this.
cout<<a[0]<<endl;
return 0;
}

The compiler has an error, prompting us to do so is not allowed.

But pinch, an array as a whole can be used as a reference to another array:

Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
int a[2];
A[0]=1;
a[1]=2;
int (&b) [2]=a;
b[0]=3;
b[1]=4;
cout<<a[0]<<endl;
cout<<a[1]<<endl;
return 0;
}

Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
int a[2];
A[0]=1;
a[1]=2;
int (&b) [2]=a;
b[0]=3;
b[1]=4;
cout<<a[0]<<endl;
cout<<a[1]<<endl;
return 0;
}

Because the elements in a numeric value are not references to other variables, you need to use (&b) to illustrate that this is an array-whole reference, and then you must also write a clear array size, i.e.:
Copy Code code as follows:

(int (&B) [2])

The use of this method also leads to the use of the array as a function of the parameters of the method.

Let's look at the array as a function of the parameters of the time it should be.

Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
void sm (int (&b) [2]);
int a[2];
A[0]=1;
a[1]=2;
SM (a);
cout<<a[0]<<endl;
cout<<a[1]<<endl;
return 0;
}
void sm (int (&b) [2]) {
b[0]=3;
b[1]=4;
}

The output result is

3

4

Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
int a[2];
A[0]=1;
a[1]=2;
int (&b) [2]=a;
b[0]=3;
b[1]=4;
cout<<a[0]<<endl;
cout<<a[1]<<endl;
return 0;
}

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.