问题描述:

This question already has an answer here:

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

    3 answers

网友答案:

sizeof input / sizeof input[0] is not going to give you the size of the array. Once you pass an array as a pointer to a function you lose all ability to get the array size from the pointer.

To fix this you either need to pass the size of the array into the function or you can pass beginning and end pointers. You could also use a template which will get the size for you like:

template<typename T, size_t SIZE>
void ReadArray(T (&input)[SIZE]) {
    std::string str( input, input + SIZE);
    std::cout << str << std::endl;
}

Live Example

相关阅读:
Top