如何找到整数数组的大小 [英] How to find the size of integer array

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

问题描述

如何找到一个整数数组的大小用C

How to find the size of an integer array in C.

而无需遍历整个数组一次,找出数组的大小可用任何方法。

Any method available without traversing the whole array once, to find out the size of the array.

推荐答案

如果数组是一个全球性的,静态或自动变量( int数组[10]; ) ,那么的sizeof(阵列)/的sizeof(数组[0])的作品。

If the array is a global, static, or automatic variable (int array[10];), then sizeof(array)/sizeof(array[0]) works.

如果它是一个动态分配的数组(为int *阵列=的malloc(sizeof的(INT)* 10); )或函数参数传递(无效F(int数组[])),那么你就无法找到它在运行时的大小。你将不得不在一些地方保存的大小。结果
需要注意的是的sizeof(阵列)/的sizeof(数组[0])编译就好了,即使是第二种情况,但它会悄悄地产生错误的结果。

If it is a dynamically allocated array (int* array = malloc(sizeof(int)*10);) or passed as a function argument (void f(int array[])), then you cannot find its size at run-time. You will have to store the size somewhere.
Note that sizeof(array)/sizeof(array[0]) compiles just fine even for the second case, but it will silently produce the wrong result.

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

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