首页 > 解决方案 > 如何在 C++ 中获取 const 数组的大小?

问题描述

#include <iostream>
using namespace std;

const int triTable[4][6] = {
    {},
    {0, 8, 3},
    {0, 1, 9},
    {1, 8, 3, 9, 8, 1},
};

int main() {
  const int *f = triTable[3];
  int size = sizeof(f)/sizeof(*f);
  cout << size; 
}

这给了我错误的数字。它应该打印6。我怎么才能得到它?

标签: c++arrayssize

解决方案


如何在 C++ 中获取 const 数组的大小?

像这样:

std::size(triTable[3])

这给了我错误的数字。

您通常无法通过使用指向该数组元素的指针来获取数组的大小。


推荐阅读