views:

423

answers:

2

Given the following program

#include <iostream>

using namespace std;

void foo( char a[100] )
{
  cout << "foo() " << sizeof( a ) << endl;
}

int main()
{
  char bar[100] = { 0 };
  cout << "main() " << sizeof( bar ) << endl;
  foo( bar );
  return 0;
}

outputs

main() 100
foo() 4

The questions:

  1. Why is the array passed as a pointer to the first element ?
  2. Is it a heritage from C ?
  3. What does the standard say ?
  4. Why is the strict type-safety of C++ dropped ?
+5  A: 

Yes. In C and C++ you cannot pass arrays to functions. That's just the way it is.

Why are you doing plain arrays anyway? Have you looked at boost/std::tr1::array or std::vector?

Note that you can, however, pass a reference to an array of arbitrary length to a function template. Off the top of my head:

template< std::size_t N >
void f(char (&arr)[N])
{
  std::cout << sizeof(arr) << '\n';
}
sbi
But you can pass in "reference to array".
Richard Corden
Just some legacy code
CsTamas
@Richard: I was just adding this while you wrote your comment. `:)`
sbi
Passing by reference to array is not limited to "function templates". You can pass an array by reference to non template functions. The advantage of using a function template is that you can deduce the array index, thereby allowing that you can call the function for different sized array types.
Richard Corden
If I'd put the array in a struct and pass it to the function, it'd report the same size. So the rules for passing C arrays and objects as parameters are different ?
CsTamas
@CsTamas, yes, the rules for passing arrays and objects are different in C. Structures are actually copied by value when passed as a parameter. Arrays are treated as a pointer to their first element. (Arrays and pointers in C are very inter-related. They aren't the same thing, but for purposes of parameter-passing they are identical)
Tyler McHenry
@CsTomas: In 8.3.5/3 the standard describes the rules that are applied to parameters when a function is declared. These rules decide if the function is a redeclaration or an overload. This section explicitly covers where the parameter type is an array (or function) and this is why the behaviour differs between the array and structure case.
Richard Corden
@Richard: I know. (Also see my comment to http://stackoverflow.com/questions/1328223/sizeof-array-passed-as-parameter/1328246#1328246.) However, since the question was about passing array sizes, I suppose CsTamas wants to pass arrays of arbitrary length. I have edited my answer to emphasize that.
sbi
+12  A: 

Yes its a heritage from C. The function:

void foo ( char a[100] );

Will have the parameter decay to a pointer, and so becomes:

void foo ( char * a );

If you want that the array type is preserved, you should pass in a reference to the array:

void foo ( char (&a)[100] );

C++ '03 8.3.5/3:

...The type of a function is determined using the following rules. The type of each parameter is determined from its own decl-specifier-seq and declarator. After determining the type of each parameter, any parameter of type "array of T" or "function returning T" is adjusted to be "pointer to T" or "pointer to function returning T," respectively....

To explain the syntax:

Check for "right-left" rule in google, I found one description of it here.

It would be applied to this example approximately as follows:

void foo (char (&a)[100]);

Start at identifier 'a'

'a' is a

Move right - we find a ) so we reverse direction looking for the (. As we move left we pass &

'a' is a reference

After the & we reach the opening ( so we reverse again and look right. We now see [100]

'a' is a reference to an array of 100

And we reverse direction again until we reach char:

'a' is a reference to an array of 100 chars

Richard Corden
The latter has the disadvantage of hardwiring the array size into the function signature. A function template can avoid that.
sbi
On a somewhat related note, could someone clear up the syntax of the above for me? I'm obviously missing something, but I don't quite see how that evaluates to a reference to an array; it looks more like an array of references.
suszterpatt
it's probably worth mentioning that using a std::vector will neatly side step all of the problems related to passing arrays.
markh44
This boils down to the fact that plain array parameters in C/C++ are a fiction - they're really pointers. Array parameters should be avoided as much as possible - they really just confuse matters.
Michael Burr