C + + Learning Notes (iv)--about array as function parameter value passing and reference passing __jquery

Source: Internet
Author: User
Tags array length

1. First, there is a difference between an array as a function parameter and a normal parameter, we know that ordinary parameters like void Insert (int a,int b) are passed into value passing, pointer passing, pointer value passing, reference passing, and their argument passing is assigning the value of the argument to the formal parameter. But in the array, the value pass is the pointer to the address of the pass, for the array is an exception, because the array of data is too much, its one by one assignment is cumbersome and waste space, so the array as a parameter passed to the function is just the address of the first element of the array, The function uses the back element when it needs to find it by following the address and array subscript. Therefore, an array of function calls can have an effect on its own array, which is different from the value passing. Also, arrays and pointers as function parameters can be interchanged, and this conversion is limited to the declaration of function parameters. This is a convenience, when passing the array will be transformed into a pointer, in fact the array has never passed the function.
Eg: The following 3 declarations are equivalent

void Insert (int *);
 void Insert (int []);
 void Insert (int [10]);

The array length is not part of the parameter type. Neither the function nor the compiler knows the size of the length of the array that was traditionally given to it.


2. What if we want to pass in the specified length array?

We can declare parameters as references to arrays, at which point the length of the array becomes part of the formal parameters and arguments.

eg:void Insert (int (&arr) [10])


3. About Object Arrays


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.