c++ - 查找 double * 数组的大小 C++

标签 c++ c arrays double

如果我声明了double *positions[2]数组,有没有办法找到 double *positions[2] 的实际大小数组?

有没有像sizeof这样的函数来查找尺寸?

我尝试过一个示例,它不会返回大小为 4

http://ideone.com/vp3tJz

最佳答案

为什么不使用sizeof? 表达方式

sizeof( positions )

将返回数组的大小(以字节为单位),等于 2 * sizeof( double * ) 或者您可以使用标准类 std::extent 来确定数组中的元素数量。例如

std::cout << std::extent<decltype( positions )>::value << std::endl;

对于您编辑的帖子,数组的声明与您显示的值不对应。该数组被定义为将指针作为其元素而不是 double 值。

如果你想定义一个用值初始化的数组

0.1, 2.5
10.0, 56.7
12.4, 20.1
69.9, 69.9
72.1, 45.0

那么你应该写

double positions[5][2] =
{
    { 0.1, 2.5 },
    { 10.0, 56.7 },
    { 12.4, 20.1 },
    { 69.9, 69.9 },
    { 72.1, 45.0 }
};

在这种情况下,sizeof(positions)将等于5 * 2 * sizeof(double)

或者

sizeof( position ) / sizeof( *positions )

将等于 5 并且

sizeof( *position ) / sizeof( **positions )

等于 2

关于c++ - 查找 double * 数组的大小 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27314979/

相关文章:

c++ - 如何将单个位转换为字符?

c++ - 在 C++ 中对列(数组)的平均值进行排序和打印

c++ - while (cin>> struct str) 在 C++ 中我该如何使用?

javascript - $.parseJSON() 可以处理的最大长度是多少?

c++ - 如何到达字符串中的第二个单词?

c++ - 将 CPP 定义移植到 python

c - fprintf 到文件和标准输出

c - 双向链表插入特定列表函数和参数

java - 如何使用坐标移动数组中的符号?

python - Numpy 数组均值函数在均值计算中未排除屏蔽元素