C + + Array reference one, array reference
References to C + + arrays:
Reference is Alias
It's more convenient than a pointer-to-address.
The (&a) [10] in the formal parameter can be regarded as an alias of the A array, and the size of the array must be specified, if there is no subsequent array size, God knows if it is a variable or an array
Comparison of normal and quoted values:
Array Normal pass value: a-->a[10] (or a[])
Array reference value:a--> (&a) [10]
Variable normal value: A-->a
Variable reference value:a--> (&a)
Analogy, well understood
Second, the code example
1 /*2 references to C + + arrays:3 Reference is alias4 It's a lot easier than sending addresses.5 The (&a) [10] in the formal parameter can be regarded as an alias of the A array, and the size of the array must be specified, if there is no subsequent array size, God knows if it is a variable or an array6 comparison of Normal and quoted values:7 array Normal pass value: a-->a[10] (or a[])8 Array Reference value:a--> (&A) [Ten]9 variable normal value: A-->aTen variable reference value:a--> (&a) One analogy, well understood A - Test Results: - 0 0 0 0 0 0 0 0 0 0 the 0 1 2 3 4 5 6 7 8 9 - */ - -#include <iostream> + using namespacestd; - voidTestinta[Ten]);//array Normal pass value for printing the value of array a + voidReadData (int(&a) [Ten]);//Array Reference value: Assigns a array to 0--9 A voidPrintdata (int(&a) [Ten]);//Array Reference Pass value: Used to print the value of array a at voidTestinta[Ten]){//array Normal pass value for printing the value of array a - for(intI=0;i<Ten; i++) - { -printf"%d", A[i]); - } -printf"\ n"); in } - voidReadData (int(&a) [Ten]){//Array Reference value: Assigns a array to 0--9 to for(intI=0;i<Ten; i++) + { -a[i]=i; the } * } $ voidPrintdata (int(&a) [Ten]){//Array Reference Pass value: Used to print the value of array aPanax Notoginseng for(intI=0;i<Ten; i++) - { theprintf"%d", A[i]); + } A } the intMain () { + inta[Ten]={0}; Assigns an initial value of 0 to each of the array a -Test (a);//array Normal pass value for printing the value of array a $ReadData (a);//Array Reference value: Assigns a array to 0--9 $Printdata (a);//Array Reference Pass value: Used to print the value of array a - - return 0; the}
Third, test results
Test results:
0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9
C + + Array reference