C + + Array reference

Source: Internet
Author: User

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

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.