In my understanding array
in int array[]={1,2,3,4,5}
is just a pointer to the first element of array
. It means that array
can be assigned to a pointer ptr
of type int*
.
Parameter int* &p
in hoo
will pass the argument by reference. It means we can change the passed argument to point to another value from within the hoo
.
void hoo(int* &p, int n)
{
for (int i = 0; i < n; i++)
cout << p[i] << endl;
}
int main()
{
int array[] = { 1,2,3,4,5 };
// I can do this
int* ptr = array;
hoo(ptr, 5);
// but not this.
//hoo(array, 5);
}
Question
Why can't we pass int array
to hoo
without ptr
?