Passing a reference to an array this solves the problem of passing length.

Source: Internet
Author: User
how to use array parameters

If a function takes a one-dimensional array as a parameter, we can declare this function

void func (int* a);
void func (int a[]);
void func (int a[3]);

In fact, these three forms are equivalent, and when using arrays for arguments, the compiler automatically converts the array name to a pointer to the first element of the array. This is to say from the way the parameters are passed, there are three ways to pass the parameters, pass by value, pass by pointer, pass by reference, respectively, as follows

void Test (int a);
void Test (int* a);
void Test (int& a);

The first way is to pass a copy of a

The second way is to pass a copy of the pointer pointing to a

The third way is to pass a copy of the reference to a

Since all are replicas, there is a copy-to-process, but the array cannot be copied directly, that is, not as follows

int a[3] = {1, 2, 3};
int b[] (a);    Error
int b[3];
b = A;            Error
You cannot initialize another array with one array, you cannot assign an array directly to another array, and if you want to copy an array, the only way is to copy the elements individually.
int a[3] = {1, 2, 3};
int b[3];
for (int i = 0; i < 3; ++i)
{
    B[i] = A[i];
}

Since the array cannot be copied, how can the arguments be passed? The compiler then converts the array name to a pointer to the first element, which is a copy of the pointer. But it also raises another question. We cannot know the number of array elements by the array name alone. Look at the code below.

void Test (int a[3])
{
    for (int i = 0; i < 5; ++i)
    {
        cout << A[i] << Endl;
    }
}

Clearly only the array of three elements is passed, why output 5 elements. As I said earlier, the array is converted to a pointer to the first element, so the code above is the same as the one below

void Test (int* a)//I just know A is a pointer, and I don't know how many elements a points to
{
    for (int i = 0; i < 5; ++i)
    {
        cout << A[i] << Endl;
    }
}

The compiler doesn't know how many elements an array A has, and it doesn't even realize that a is an array. How to solve it, one way is to add a parameter, specify the number of elements

void Test (int* a, int n)
{
    for (int i = 0; i < n; ++i)
    {
        cout << A[i] << Endl;
    }
}

Another way is to pass the array of references, which is the focus of this article, alas, before so much nonsense: (

void Test (int (&a) [3])
{
    for (int i = 0; i < 3; ++i)
    {
        cout << A[i] << Endl;
    }
}

So that the array a will not be converted to a pointer, and the number of elements of the information, when called, you must also pass an array containing 3 elements

int a[3] = {1, 2, 3};
Test (a);    Ok
int b[1] = {1};
Test (b);    Error, can not convert parameter a from int[1] to int (&) [3]

And that's all, Happy coding!

==the end==

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.