C与C++找到数组的大小

size of array = sizeof(array)/sizeof(data type)
size of two Dimension array = sizeof(array)/(column * sizeof(data type))

一维数组的大小:

#include <iostream>
using namespace std;
int main()
{
    int a[6] = {1, 2, 3, 4, 5, 6};
    int size = sizeof(a)/sizeof(int);
    cout << "sizeof(a) = " << sizeof(a) << " "
         << "sizeof(int) = " << sizeof(int) << " "
         << "size = " << size << endl;
         system("pause");
         return 0;
}

输出结果
在这里插入图片描述

二维维数组的大小:

#include <iostream>
using namespace std;
int main()
{
    int a[][6] = {1, 2, 3, 4, 5, 6 ,0}; //二维数组,但行数为明确标出
    int size = sizeof(a)/(6*sizeof(int));
    cout << "sizeof(a) = " << sizeof(a) << " "
         << "sizeof(int) = " << 6*sizeof(int) << " "
         << "size = " << size << endl;
         system("pause");
         return 0;
}

输出结果:
在这里插入图片描述

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试
应支付0元
点击重新获取
扫码支付

支付成功即可阅读