Jumbo said:
But that's just what an array is.
Not true. An array is an array. A pointer is a pointer. There are some
syntactic similarities in how they are used*, but there are very
important differences between the two.
*Actually, the only reason for these similarities is the implicit
conversion of an array name to a pointer in most contexts.
Just because an array happens to be turned into a pointer by the compiler
when you pass it to a function deoesn't mean to say that we're not passing
an array to a function.
Of course it does. If you're passing a pointer then you aren't passing
an array. There's not some magic dual type that is simultaneously a
pointer and an array.
We're still passing an array to a function and it's
not a copy. So as we are passing the actually array and not a copy it even
more true to say we ARE passsing an array to a function.
This is simply not true, as you can demonstrate easily enough:
#include <iostream>
void pass_array(int a[])
{
std::cout << "sizeof(a) = " << sizeof(a) << std::endl;
}
int main()
{
int array[100];
std::cout << "sizeof(array) = " << sizeof(array) << std::endl;
pass_array(array);
return 0;
}
On my implementation I get this output:
sizeof(array) = 400
sizeof(a) = 4
How can you account for this if 'a' really is 'array'? It's quite easy
to account for if 'a' is actually a pointer (which it is). Also, you can
change the signature for pass_array to any of these:
void pass_array(int *a)
void pass_array(int a[100])
void pass_array(int a[7])
And you'll get the same results, because all four mean exactly the same
thing. 'a' is actually a pointer.
You seem to be meaning that passing an array to a function is only passing
the array if it passes a copy.
I doubt that's what he was trying to say.
I would consider it to be the other way around where "passing the array"
would be passing by ref and passing a copy your aren't actally passing the
array your passing a copy..
OK, but that's not what's happening, either. You *can* pass a reference
to an array to a function if you want to, but this is not done very
often because it's of limited usefulness - the function is specific to a
particular size of array (unless you make the size a template argument).
For example, if I change my previous example so that pass_array's
signature is this:
void pass_array(int (&a)[100])
Then I get the following output:
sizeof(array) = 400
sizeof(a) = 400