如何获得 "static const unsigned char my_var[]" 的长度
How to get length of "static const unsigned char my_var[]"
我的车是:
static const unsigned char joey[] =
{
0xFF,
0xD8,
0xFF,
0xE1
};
我怎样才能得到它的长度?
对于数组大小,你可以这样做
std::size_t len = sizeof(joey) / sizeof(joey[0]);
或者更安全的方式
template <typename T, std::size_t N>
constexpr std::size_t get_size(const T(&)[N]) { return N; }
std::size_t len = get_size(joey);
我的车是:
static const unsigned char joey[] =
{
0xFF,
0xD8,
0xFF,
0xE1
};
我怎样才能得到它的长度?
对于数组大小,你可以这样做
std::size_t len = sizeof(joey) / sizeof(joey[0]);
或者更安全的方式
template <typename T, std::size_t N>
constexpr std::size_t get_size(const T(&)[N]) { return N; }
std::size_t len = get_size(joey);