为什么编译器没有在参数中传递数组char*arr []的大小?

The*_*ask 6 c++ arrays c++11

为什么编译器没有在参数中传递数组char*arr []的大小?我想得到通过参数传递的数组的大小,但我想它不起作用,因为即使char *a[]是char **我的问题是为什么它,我能使它工作吗?

#include <stdio.h>
#include <stddef.h>
#include <stdio.h>

template<class T, size_t len>
constexpr size_t lengthof(T(&)[len])
{
    return len;
}
void printarr(const char *a[]);

int main()
{
    const char *a[] = { "aba", "bd", "cd" };
    printarr(a);
}

void printarr(const char *a[])
{
    for(size_t i = 0, c = lengthof(a); i < c; i++) {
        printf("str = %s\n", a[i]);
    }
}
Run Code Online (Sandbox Code Playgroud)

Ben*_*ley 7

您可以使用lengthof功能模板中使用的相同技巧使其工作.

template<size_t len>
void printarr(const char* (&a)[len])
{
    for(size_t i = 0, c = lengthof(a); i < c; i++) {
        printf("str = %s\n", a[i]);
    }
}
Run Code Online (Sandbox Code Playgroud)