c++ -数组的计数与声明的大小无关
C++ - the count of the array is irrelevant to the declared size
我编写了以下代码块。但是sizeof()
返回的数组的大小是无关的。我有一个包含6个元素的数组,而sizeof()
返回24作为数组的大小。
代码如下:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int arr[6] = { 0, 5, 8, 32, 100, 41 };
int arrSize = sizeof(arr);
int i = 0;
cout << "This is the Size of Array: " << arrSize << endl;
/*
for (i = 0; i < arrSize; ++i)
{
cout << arr[i] << endl;
} */
return 0;
}
下面是输出:
This is the Size of Array: 24
你应该这样做
int size = sizeof(arr)/sizeof(arr[0]);
这将查找数组的总大小。这里是6 * 4 = 24
,然后除以单个元素的大小,因此24/4 = 6
sizeof
给出了数据的大小,以字节为单位
6 * 4字节整数= 24 字节
相关文章:
- 在类c++中使用new声明数组
- 为什么不能用常量表达式声明数组?
- 声明数组后我无法添加数组
- 可以使用<size_t>static_const(),std::as_const()或static_cast<const size_t>()来声明数组吗?
- 变量未在此范围内声明 数组线性搜索
- 在类中声明数组. C++
- T D[N] 总是声明数组类型的对象吗?
- 如何使用智能指针在二维上声明数组?
- 声明数组>> int arr[] 时出错;在C++(虽然在 Java 中有效)?
- 在C 类中声明数组数据
- 声明数组时出现 SIGSEGV 错误,而不创建新的 int[size]
- 如何在不初始化常量的情况下声明数组?
- 使用函数在函数中声明数组值
- 以C++声明数组的时间复杂度
- 通过传递数组长度在函数内声明数组
- 通过引用传递数组,而不在函数声明中显式声明数组的大小
- 声明数组和打印[错误?]
- 用内核参数在CUDA内核中声明数组
- 如何在不依赖#Define的情况下转发声明数组大小
- 声明数组已经填充了零