如何计算传递给函数的数组的大小 [英] how to calculate size of array which is passed to a function

查看:180
本文介绍了如何计算传递给函数的数组的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看下面的代码

void fun( int *p )
{
    // is there any solution to calculate size of array as we are not explicitly passing its size.
}

void main ()
{
    int a[5];
    fun (a);
}

有没有解决方法来计算函数中数组的大小,或者显式传递其大小是唯一的解决方法?

Is there any solution to calculate size of array in function or explicitly passing its size is the only solution?

推荐答案

您不能.

它只是一个指针,不包含数组的大小信息.这就是为什么将数组的大小作为第二个参数传递给函数的常见实现的原因.

It's just one pointer, which contains no size info of the array. That's why it's a common implementation to pass the size of the array as a second parameter to the function.

这篇关于如何计算传递给函数的数组的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆