The so-called array reference refers to the array reference;
For example, int A [10];
INT (& B) [10] =;
If it is written as int A [10];
Int * & B =;
The following error occurs: cannot convert from 'int [10] 'to 'int *&'.
Maybe you will say that the array name is not a pointer to this array? In the question, A is of the int * type and B is a reference to the int * type. The reason should be correct. Why is an error reported? This is because the compiler checks references to arrays more strictly and needs to check the dimension of the array. Here, a is interpreted as a pointer to 10 int arrays, int [10]. for reference, the corresponding reference type int (&) [10] is also required, that is, the reference to the pointer to 10 int arrays. There is an "array price reduction" issue in C and C ++. # Include <iostream> void test (char arr [1, 100])
{
STD: cout <sizeof (ARR) <STD: Endl; // output 4
} Int main ()
{
Char arr [100] = {0 };
STD: cout <sizeof (ARR) <STD: Endl; // output 100
Test (ARR); Return 0;
} This section Code The output is 100
4 For the same arr, one output is 100, and the other output is 4. It is because the ARR in void test (char arr [100]) is reduced. The ARR in void test (char arr [100]) is downgraded,
Void test (char arr [100]) is equivalent to void test (char arr []) and
Void test (char * const ARR) is equivalent
Void test (char arr [10])
The compiler does not check the dimension of the array. That is to say
Void test (char arr [1, 100])
{
STD: cout <sizeof (ARR) <STD: Endl;
} Dropped
Void test (char * const ARR)
{STD: cout <sizeof (ARR) <STD: Endl; // since it is char *, of course, output 4
} In this way, the size of the array is not checked. Program This will cause problems. How can this problem be solved? You can use references to arrays in C ++.
See the following code:... void test (const char (& ARR) [100])
{
STD: cout <sizeof (ARR) <STD: Endl; // output 100
}......
Char arr [100] = {0 };
STD: cout <sizeof (ARR) <STD: Endl; // output 100
Test (ARR );
In this way, test can accept an array of 100 char, and can only accept a char array of 100 size if
Char arr [20] = {0 };
Test (ARR );
An error is reported in C ++. You can directly pass the array name for array reference, because the array size information is provided in the form parameter. However, we can only use this function to fix the size of the array. You can solve this problem by adding array references to the template. See the following code: Template <int SZ>
Void test (char (& ARR) [SZ])
{
For (INT I = 0; I <SZ; I ++)
......} Char A [2] = {0}, B [15] = {0}; test (a); // oktest (B); // OK ...... this solves the problem of variable array length, but introduces new problems: (1) when there are multiple test calls, multiple test codes will be generated. Traditional function calls have only one generation, and the number of calls is irrelevant.
(2) because the code is generated during the compilation phase, it needs to know the size of the referenced array. Therefore, a function written in this way obviously cannot use pointer variables as function parameters. Therefore, the function cannot be used to process dynamically allocated memory areas. The size of such areas is determined at runtime.
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