I am just learning C++ and a little confused about arrays and references. I wrote the following program:
void printArray(int arr[]) {
cout << arr[0] << arr[1] << arr[2] << endl;
}
int main() {
int arr[3] = {5, 7, 9};
int *aPtr = &arr[0];
cout << aPtr[0] << aPtr[1] << aPtr[2] << endl;
int *bPtr = arr;
cout << bPtr[0] << bPtr[1] << bPtr[2] << endl;
printArray(arr);
}
The output is :
579
579
579
And I have two questions:
- Is array in C++ a reference? I mean, can I state that
i = &i[0]
? - If answer to the first question is yes, can I say that as array is reference we don't need to use
&
in arguments of the functionprintArray
. I mean, we don't declare the function this wayprintArray(int &arr[])
?