A collection of questions about arrays as function parameters

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

The first is an array element as an argument to a function, which is no different from using multiple variables as arguments to a function.

As an example of the code:


#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 is: 2

Then, the function takes the array name, which actually represents a pointer to the first element of the array.


#include<iostream>
using namespace std;
int main(){
        void alter(int b[]);//The value in the b[] bracket is writable, which causes the compiler 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 is:

3

4

If we write it like this:


#include<iostream>
using namespace std;
int main(){
        void alter(int b[20]);//The value in the b[] bracket is writable, which causes the compiler 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 is it that we've already defined a[2] and assigned a value, and when we pass it to the function, the size is only one unit?

In fact, we define b[] or b[2] or b[1], b[20], b[100], all of which are equivalent to *b. The compiler simply ignores the values in the parentheses.

To further illustrate, the array name as an argument is actually the first pointer to the passed array, take a look at this example again:


#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 as the output above!

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = line = = = = = = = = = = = = = = = = = = = = = = = = = =

Next, a summary of the problems associated with array references

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


#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 is: 5

When a normal variable is used as a reference to a value in an array, it is used with int &a=b; There is no difference.

It is natural to think that ordinary variables can be used as references to array elements, but can array elements be used as references to other elements?

Look at the following code:


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

The compiler reported an error, indicating that we were not allowed to do so.

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


#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;   
}

The same code at the page code block index 6
Since the element in the value is the amount that cannot be referenced by other variables, we need to use (&b) to indicate that this is the whole reference of the array, and then we must write clearly the size of the array, that is:

(int (&b)[2] ) 

This use method also leads to the use of an array as a parameter of a function.

Let's look at what an array should look like as a parameter of a function.


#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 is

3

4
The same code at the page code block index 6


Related articles: