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

前端 未结 3 1251
南笙
南笙 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 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';
    }
    

提交回复
热议问题