c++ - 如何在 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++ 中获取 const 数组的大小?
像这样:
std::size(triTable[3])
这给了我错误的数字。
您通常无法通过使用指向该数组元素的指针来获取数组的大小。
推荐阅读
- twilio - Twilio:在 bin 之间传递参数
- python - 如何在 ModelSerializer 中修复“此函数的无效关键字参数”
- oracle - 如何让我的整个列数据作为用户登录我的应用程序?
- javascript - 如何自动将文件夹上传到天蓝色?
- html - adoc 上的选框包含文本文件
- c++ - 如何避免在单元测试中调用方法?
- c - 为什么在函数声明中传递 const 指针参数无效
- date - Yii2:如何更改 2amigos yii2-date-picker-widget 中的日期格式?
- java - JAVA SFTP:虽然通过 JCH 覆盖文件。收到错误“错误消息”
- pyspark - 使用 MagicMock 修补 PySpark 中的函数不会在 spark 运行中修补