I'm new to c++ and I am confused by the idea of passing a pointer array into a function.
Here's the function
void func(int *a){ a[0]=999;}
which is used by the main
int main()
{
int a[5]={1,2,3,4,5};
func(a);
std::cout << a[0]<< std::endl;
}
I understand this works perfectly as the name of an array is just the address of its first element.
Based on my understanding, &a
refers to the address of the entire array while a
refers to the address of the first element of the array, which should be identical to &a
.
However, if I use
int main(){
int a[5]={1,2,3,4,5};
func(&a);
cout<< a[0]<< endl;
}
It returns the compiler error: no known conversion from 'int (*)[10]' to 'int *' for 1st argument; remove &
Could anyone please explain what's going on here?