quote:
Original post by smart_idiot
Well, my compiler can''t seem to tell the difference, but maybe it''s broken. Please enlighten it as to why it shouldn''t be compiling this program (besides that it was written by me):
You don''t know the language well enough:
void func2(int foo[4]);
Does not make a function that takes as a parameter an array of length 4. It makes a function that takes a pointer to an integer. In fact, that 4 in your declaration means absolutely nothing. The same exact declaration can be done with:
void func2(int* foo);
and
void func2(int foo[]);
Why? First, remember that arrays don''t have copy constructors, which should have been your first tip off, so obviously from the start the function declaration couldn''t be doing what you''d think it''s doing from what you wrote out. The reason that C++ provides the syntax
int foo[]
is so that the person reading the function declaration can clearly see that an array is meant to be passed via a pointer to the first element. If you just used
int* foo
It won''t be as clear from the start that you want an array to be passed.
If you REALLY wanted to test out if arrays and pointers were the same you could do make 2 different functions -- one which takes a reference to an array and one which takes a reference to a pointer. If they really were the same then a reference to an array would be the same as a reference to a pointer.
void func1(int*& foo); // Parameter is a reference to a pointer
void func2(int (&foo)[4]); // Parameter is a reference to an array of length 4
You may have thought you knew what your code was doing but you didn''t. This is a more accurate representation of what you were trying to prove:
void func1(int*& foo); // Parameter is a reference to a pointervoid func2(int (&foo)[4]); // Parameter is a reference to an array of length 4void (*func[2])(int (&foo)[4]) = {&func1, &func2}; // Error on func1 because an array is not the same as a pointer