小编Sni*_*gdh的帖子

查找作为参数传递给函数的数组中的元素数.

我试图创建一个函数来查找数组中的元素数量.为此我找到了以下代码:

#include<iostream>
#include<stdlib>

int no_of_ele(A[])    //function to return size of array
{
    return (sizeof(A)/sizeof(A[0]);
}

void main()
{
    system("cls");
    int arr[5] = {1,2,3,4,5};

    cout<<"Number of elements in array are "<<no_of_ele(arr)<<endl;
    system("pause");
}
Run Code Online (Sandbox Code Playgroud)

在这种方法中,我得到如下输出:
在此输入图像描述

然后,我这样做了:

cout<<"Size of array is "<<sizeof(arr)<<endl;
cout<<"Size of data type is "<<sizeof(arr[0]);
Run Code Online (Sandbox Code Playgroud)

现在我得到绝对正确的大小输出如下:

在此输入图像描述

为什么?

c++ sizeof turbo-c++

3
推荐指数
1
解决办法
722
查看次数

标签 统计

c++ ×1

sizeof ×1

turbo-c++ ×1