Reference to Array in C++
Reference to an array means aliasing an array while retaining its identity. Reference to an array will not be an int* but an int[]. Let us discuss this in detail by discussing the difference between these two. This is quite weird that int[] is the same as int* but still compiler perspective on both is entirely different. The major two differences are as follows:
- int[] for the compiler is an array, so it provides an iterator for this (that’s why we have For-Each Loop) but int* is just a pointer to an integer. This could just be a pointer to integer or pointer to the beginning of integer array which totally depends upon our perspective. Hence, no For-Each loop in this case.
- For compiler, a and b are the same data type i.e int* here for compiler they are just int* pointing to address. But for compiler type of as an array is int[2] and type of b as an array is int[3] which are completely different from each other. Again just the compiler’s perspective. For better understanding let the two arrays be int a[2], int b[3].
Implementation:
- Passing an array to function in a classical way.
- Later on, trying to call for-each loop on it, a clear difference can be obtained.
Example:
C++
#include <iostream>
using namespace std;
void print( int arr[], size_t n)
{
for ( int element : arr) {
cout << element << " " ;
}
cout << endl;
}
int main()
{
int a[]{ 1, 2, 3, 4 };
size_t size = sizeof (a) / sizeof (a[0]);
print(a, size);
}
|
Output:
test.cpp: In function 'void print(int*, size_t)':
test.cpp:5:21: error: 'begin' was not declared in this scope
for(int element: arr){
Output Explanation:
Here it is clear that int* doesn’t have any information about the underlying array but if you pass an array by reference using the template the above code will work. As reference array retains information about underlying array and its type would be int[4], not int*.
Now let us discuss a reference to an array.
Methods:
- Naive method
- Reference to an Array
Method 1: Naive method
First most the common way that comes into our mind is described below syntactically. This is clearly a Naive approach as it loses its array identity.
int a[] = {1, 2, 3, 4};
int *b = a;
Note: int a[] = b; will not work as array can only be initialized using aggregate object
Method 2: Reference to Array
- The size needs to be mentioned as int[x] and int[y] are different data types from the compiler’s perspective.
- Reference to array needs to be initialized at the time of declaration.
- (&name) is not redundant. It has its own meaning.
Syntax:
data_type (&name)[size] = array;
Note: data_type &name[size] is incorrect because it means an array of reference to some datatype which is clearly meaningless. By doing so we have “name” of data type int (&) [] which is, of course, different from int[]
Example:
C++
#include <iostream>
using namespace std;
int main()
{
int a[]{ 1, 2, 3, 4 };
int (&b)[4] = a;
for ( int e : b) {
cout << e << " " ;
}
}
|
Last Updated :
29 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...