1

According to http://en.cppreference.com/w/cpp/language/type , arrays of unknown bound are incomplete types and function definitions are only allowed with arguments of complete types.

Well, consider the following code:

#include <iostream>
using namespace std;

void f(int t[])
{
    cout << t[2] << endl;
}

int main()
{
    int tab[5] = {1,2,3,4,5};
    f(tab);
}

To my understanding, int t[] is an array of unknown bound. Therefore no argument of such type should appear within the definition of function f, and the above code should be illegal.

Yet both clang http://melpon.org/wandbox/permlink/IqOXuXBqIsJSJLOr and gcc http://melpon.org/wandbox/permlink/8TKWq3UTP5sNC8rJ accept this code. Why?

  • 1
    Nitpick: you're passing an argument of a complete type. You're asking about the parameter, not the argument. – chris Dec 16 '15 at 19:57
  • @chris I wonder if this is a nitpick or the answer to my question… The exact quote of cppreference.com: "*Any of the following contexts requires class T to be complete: (...) * definition or function call to a function with return type T or argument type T; (...)*" –  Dec 16 '15 at 20:01
  • A function definition, like you have, requires its parameters to be complete. A declaration does not. The actual answer is that it's really turned into a pointer and a pointer is a complete type. – chris Dec 16 '15 at 20:06
  • Another dup: http://stackoverflow.com/questions/14309136/passing-arrays-to-function-in-c – Jonathan Wakely Dec 16 '15 at 20:08

1 Answers1

2

The usual rules for adjustment of parameter types apply to arrays of unknown bound too. Therefore, that declaration actually makes f a function taking an argument of type int*, not int[]. int* is a complete type.

Brian Bi
  • 111,498
  • 10
  • 176
  • 312