I'm trying to learn how to use pointers and references using a video course and I find pointers and references to be quite complex.
I'm trying to make a really basic exercise so I can get how it works. It looks like this:
void print(int &array, int size)
{
for (int i = 0; i < size; i++)
{
cout << array[i] << " ";
}
}
int main()
{
int n, a[10];
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
print(a, 5);
return 0;
}
It is not working because I'm passing an array as a reference. If I change the function prototype to this
void print(int *array, int size)
it works perfectly.
Can anyone explain me why? I think in this situation using a pointer or a reference should be the same. Both would lead to the array. Am I wrong?