When a function has a specific-size array parameter, why is it replaced with a pointer?

前端 未结 3 1243
南笙
南笙 2020-11-21 05:43

Given the following program,

#include 

using namespace std;

void foo( char a[100] )
{
    cout << \"foo() \" << sizeof( a ) <         


        
相关标签:
3条回答
  • 2020-11-21 05:53

    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

    0 讨论(0)
  • 2020-11-21 06:02

    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';
    }
    
    0 讨论(0)
  • 2020-11-21 06:02

    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
    
    0 讨论(0)
提交回复
热议问题