通过使用函数计算数组长度用C [英] Calculate Length of Array in C by Using Function

查看:160
本文介绍了通过使用函数计算数组长度用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让它计算通过数组大小的函数。

I want to make a FUNCTION which calculates size of passed array.

我将传递一个数组作为输入,它应该返回其长度。我希望有一个功能

I will pass an Array as input and it should return its length. I want a Function

int ArraySize(int * Array   /* Or int Array[] */)
{
   /* Calculate Length of Array and Return it */

}

void main()
{
  int MyArray[8]={1,2,3,0,5};
  int length;

  length=ArraySize(MyArray);

  printf("Size of Array: %d",length);

}

长度应为5,因为它包含5个元素,虽然它的尺寸为8
(甚至8会做,但5将是极好的)

Length should be 5 as it contains 5 elements though it's size is 8 (Even 8 will do but 5 would be excellent)

我试过这样:

int ArraySize(int * Array)
{

  return (sizeof(Array)/sizeof(int));

}

这将不能按的sizeof(阵列)将retun诠释指针的大小。
这种的sizeof 的事情只能如果你在相同的功能。

This won't work as "sizeof(Array)" will retun size of Int Pointer. This "sizeof" thing works only if you are in same function.

其实我回来到C好多天从C#后,所以我不记得了(和失踪 Array.Length()

Actually I am back to C after lots of days from C# So I can't remember (and Missing Array.Length())

祺!

推荐答案

您不能时,你所得到的是一个int指针计算一个int数组的大小。

You cannot calculate the size of an int array when all you've got is an int pointer.

让这个功能类的唯一方法是定义一个宏:

The only way to make this "function-like" is to define a macro:

#define ARRAY_SIZE( array ) sizeof( array ) / sizeof( array[0] )

这带有宏的所有常见的注意事项,当然。

This comes with all the usual caveats of macros, of course.

编辑:(下真正的意见属于成的答案...)

(The comments below really belong into the answer...)


  1. 您的不能确定元素的数量初始化的阵列中,除非你首先初始化一个无效值的所有元素和做的有效值计数手动。如果阵列已经被定义为具有8个元素,因为它有8个元素,无论你是否其中只有5初始化的编译器。

  2. 您的不能确定到该阵列已作为参数传递的函数中的数组的大小。不是直接的,而不是通过一个宏,不以任何方式。您可以的数组的大小确定的的范围已经在的声明。

  1. You cannot determine the number of elements initialized within an array, unless you initialize all elements to an "invalid" value first and doing the counting of "valid" values manually. If your array has been defined as having 8 elements, for the compiler it has 8 elements, no matter whether you initialized only 5 of them.
  2. You cannot determine the size of an array within a function to which that array has been passed as parameter. Not directly, not through a macro, not in any way. You can only determine the size of an array in the scope it has been declared in.

确定在调用的函数数组的大小是不可能的,可一旦你意识到了解, 的sizeof()编译时间的运营商即可。它可能的的像一个运行时函数调用,但它不是:本的编译的决定操作数的大小,将它们插入为常量

The impossibility of determining the size of the array in a called function can be understood once you realize that sizeof() is a compile-time operator. It might look like a run-time function call, but it isn't: The compiler determines the size of the operands, and inserts them as constants.

在该阵列被声明的范围,编译器,它实际上是一个数组,它有多少个元素的信息。

In the scope the array is declared, the compiler has the information that it is actually an array, and how many elements it has.

在到该数组传递函数,所有的编译器看到的是一个指针。 (考虑到功能可能与众多的被称为不同的的阵列,并记住的sizeof()编译时运营商

In a function to which the array is passed, all the compiler sees is a pointer. (Consider that the function might be called with many different arrays, and remember that sizeof() is a compile-time operator.

您可以切换到C ++,并使用<矢量GT; 。你可以定义一个结构向量加处理该功能,但它不是很舒服:

You can switch to C++ and use <vector>. You can define a struct vector plus functions handling that, but it's not really comfortable:

#include <stdlib.h>

typedef struct
{
    int *  _data;
    size_t _size;
} int_vector;

int_vector * create_int_vector( size_t size )
{
    int_vector * _vec = malloc( sizeof( int_vector ) );
    if ( _vec != NULL )
    {
        _vec._size = size;
        _vec._data = (int *)malloc( size * sizeof( int ) );
    }
    return _vec;
}

void destroy_int_vector( int_vector * _vec )
{
    free( _vec->_data );
    free( _vec );
}

int main()
{
    int_vector * myVector = create_int_vector( 8 );
    if ( myVector != NULL && myVector->_data != NULL )
    {
        myVector->_data[0] = ...;
        destroy_int_vector( myVector );
    }
    else if ( myVector != NULL )
    {
        free( myVector );
    }
    return 0;
}

底线:C数组是有限的。无法计算其长度在一子功能,周期。你必须code围绕这一限制你的方式,或者使用不同的语言(如C ++)。

Bottom line: C arrays are limited. You cannot calculate their length in a sub-function, period. You have to code your way around that limitation, or use a different language (like C++).

这篇关于通过使用函数计算数组长度用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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