0

For the following function definitions, whether they are in fact all equal? If not, what's the difference?

void f1(int *a)
{
    //something to do;
}

void f2(int a[])
{
    //the same as function f1
}

void f3(int a[3])
{
    //the same as function f1
}

Thanks a lot!

Daniel
  • 1,783
  • 2
  • 15
  • 25

1 Answers1

3

The function signatures are the same. You may check this out by naming all functions with one name.

void f1(int *a) {}
void f1(int a[]) {}
void f1(int a[3]) {}

Your compiler will throw a compile error saying something like "redefinition of void f1(int *)". Now notice this, if you rearrange functions as following:

void f1(int a[3]) {}
void f1(int a[]) {}
void f1(int *a) {}

You will get the same error message about "redefinition of void f1(int *)", not of "void f1(int a[3])". It is because in C++ there's only a way to pass a pointer to an array and function arguments that look like arrays are just syntax ease to pass pointers. Also check this out: Why do C and C++ compilers allow array lengths in function signatures when they're never enforced?

Community
  • 1
  • 1
Juster
  • 732
  • 1
  • 10
  • 26
  • 1
    A foot note is that they do get enforced for multidimensional arrays except where the left most one is concerned. Effectively the left most [] isn't enforced where all others dimensions require a size to be complete. As well those dimensions must match what is passed into the function. – Michael Petch Sep 16 '14 at 05:10