How to Use array parameters

Source: Internet
Author: User
How to Use array parameter http://www.cnblogs.com/graphics/archive/2010/07/15/1777760.html

If a function uses a one-dimensional array as the parameter, we can declare this function as follows:

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

In fact, these three forms are equivalent. When using arrays as parameters, the compiler will automatically convert the array name to a pointer pointing to the first element of the array. Why? Starting from the parameter transmission method, there are three transmission methods for parameters: pass by value, pass by pointer, and pass by reference, respectively, as follows:

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

The first method is to pass a copy of.

The second method transmits a copy of the pointer to.

The third method is to point to a referenced copy of.

Since they are all copies, there is a copy process, but the array cannot be copied directly, that is, it cannot be like the following

int a[3] = {1, 2, 3} ;
int b[](a) ;    // error
int b[3] ;
b = a ;            // error

You cannot use an array to initialize another array or assign an array directly to another array. To copy an array, you can copy the elements one by one.

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 we pass the parameter? The compiler converts the array name to a pointer pointing to the first element, which can be copied. However, this also raises another issue. We cannot know the number of array elements only through the array name. See the following code.

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

Why are 5 elements output when only an array of three elements is passed? As mentioned above, the array is converted into a pointer pointing to the first element, so the above Code is the same as the following

Void test (int * A) // I only know that A is a pointer and does not know how many elements a points.
{
    for (int i = 0; i < 5; ++i)
    {
        cout << a[i] << endl ;
    }
}

The compiler does not know how many elements a has. It does not even know that A is an array! How can we solve this problem? One way is to add a parameter to 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 reference, which is the focus of this Article. Alas, there are so many nonsense :(

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

In this way, array a will not be converted into a pointer, and with the information of the number of elements, an array containing three elements must be passed during the call.

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.