Possible Duplicate:
How do I use arrays in C++?
void fn(int a[3])
{
a[5]=5;
}
int main()
{
int A[10] = {0};
cout<<A[5]<<endl;
fn(A);
cout<<A[5]<<endl;
}
For the first print statement I got A[5]=0 and for the second time A[5]=5. How does this code actually work ?
In C++ array from programmer’s view is almost equal to the pointer to its first element. In case of arrays passed by parameter, they are equal. If you imagine it this way, you pass the pointer to an array to function, then modify its sixth element (still the original!), and then display it.
Your code is equal to:
Side note The difference in statically allocated and dynamically allocated arrays can be seen while playing around with local variables: