When a function has a specific-size array parameter, why is it replaced with a pointer?
Solution 1
Yes it's inherited from C. The function:
void foo ( char a[100] );
Will have the parameter adjusted to be 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
Solution 2
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
/std::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';
}
Solution 3
There is magnificent word in C/C++ terminology that is used for static arrays and function pointers - decay. Consider the following code:
int intArray[] = {1, 3, 5, 7, 11}; // static array of 5 ints
//...
void f(int a[]) {
// ...
}
// ...
f(intArray); // only pointer to the first array element is passed
int length = sizeof intArray/sizeof(int); // calculate intArray elements quantity (equals 5)
int ptrToIntSize = sizeof(*intArray); // calculate int * size on your system
Comments
-
NEO_FRESHMAN almost 2 years
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
- Why is the array passed as a pointer to the first element?
- Is it a heritage from C?
- What does the standard say?
- Why is the strict type-safety of C++ dropped?
-
Richard Corden over 14 yearsBut you can pass in "reference to array".
-
sbi over 14 years@Richard: I was just adding this while you wrote your comment.
:)
-
sbi over 14 yearsThe latter has the disadvantage of hardwiring the array size into the function signature. A function template can avoid that.
-
Richard Corden over 14 yearsPassing 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.
-
NEO_FRESHMAN over 14 yearsIf 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 ?
-
Tyler McHenry over 14 years@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)
-
Richard Corden over 14 years@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.
-
suszterpatt over 14 yearsOn 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.
-
markh44 over 14 yearsit's probably worth mentioning that using a std::vector will neatly side step all of the problems related to passing arrays.
-
sbi over 14 years@Richard: I know. (Also see my comment to stackoverflow.com/questions/1328223/…) 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.
-
Michael Burr over 14 yearsThis 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.
-
juanchopanza about 9 yearsJust a nit-pick: the function parameter doesn't decay to pointer. It is adjusted to pointer. An array name used as a function argument can decay to a pointer if the function parameter is a pointer.
-
gsamaras about 9 yearsBravo for explaining the rule, since the link is now a 404.
-
Richard Corden about 9 years@G.Samaras Nice catch. I've fixed it now. The appropriate section is 2.4.
-
gsamaras about 9 years@RichardCorden the link now works. However, I liked so much your explanation that I do not feel I need to read the link at the time. ;p Thanks for the great answer.
-
Trevor Hickey about 8 yearsThere is std::array now.
-
sbi about 8 years@Trevor Thanks, added.
-
underscore_d over 7 yearsAnd? This, at best, indirectly indicates what happens. The OP was asking why the language is set up that way. Also, "static array" is a confusing term when what you really mean is dynamically allocated; technically, the array you've shown has
extern
linkage, notstatic
. And I'm not sure what function pointers have to do with anything here?