In-depth analysis of C ++ array parameters

Source: Internet
Author: User

In C ++, an array will never be passed by value. It is the first element passed, accurate to 0th pointers.

For example, the following statement:
Void putvalues (INT [10]);
Considered by the compiler
Void putvalues (int *);
The length of the array has nothing to do with the parameter declaration. Therefore, the following three statements are equivalent:
// Three equivalent putvalues () Statements
Void putvalues (int *);
Void putvalues (INT []);
Void putvalues (INT [10]);

 

Because the array is passed as a pointerProgramThe Member has two meanings:

 
1.Changes to the parameter array in the called function will be applied to the array real parameters instead of local copying. When the Array Used as the real parameters must remain unchanged, the programmer needs to keep the copy function of the original array. By declaring the parameter type as const, the programmer does not want to change the array element.
Void putvalues (const int [10]);

2.The array length is not part of the parameter type. The function does not know the actual length of the array passed to it, and the editor does not know it. When the compiler checks the parameter type of the real parameter type, the length of the array is not checked. For example:
Void putvalues (INT [10]); // treated as int *
 
Int main (){
Int I, j [2];
Putvalues (& I); // OK: & I is int *; potential running errors
Putvalues (j); // OK: J is converted to a pointer of 0th elements.
// The real parameter type is int *: potential running error
Return 0;
}

The type check of the parameter can only ensure that both calls of putvalues () provide real parameters of the int * type. The type check cannot verify that the real parameters are an array of 10 elements. Traditionally, a C-style string is an array of characters, ending with an empty character encoding. However, all other types, including character arrays that want to process null characters, must somehow let them know its length when passing real arguments to the function.

 

A common mechanism is to provide an additional parameter with an array length. For example:
Void putvalues (INT [], int size );
Int main (){
Int I, j [2];
Putvalues (& I, 1 );
Putvalues (J, 2 );
Return 0;
}

 

Another mechanism is to declare the parameter as an array reference.

when a parameter is referenced as an array type, the array length becomes part of the parameter and real parameter type, the compiler checks whether the length of the array arguments matches the length specified in the function arguments.
// array with 10 int values
// parameter is a reference to an array of 10 ints
void putvalues (INT (& ARR) [10]); // cannot be written as & arr [10], because the subscript operator has a higher priority
int main () {
int I, J [2];
putvalues (I); // error: the real parameter is not an array of 10 int values
putvalues (j); // error: the real parameter is not an array of 10 int values
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.