Calculate Length of Array in C by Using Function

前端 未结 9 652
心在旅途
心在旅途 2020-12-04 18:20

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         


        
相关标签:
9条回答
  • 2020-12-04 18:56
    int getArraySize(void *x)
    {
        char *p = (char *)x;
        char i = 0;
        char dynamic_char = 0xfd;
        char static_char = 0xcc;
    
        while(1)
        {
            if(p[i]==dynamic_char || p[i]==static_char)
                break;
            i++;
        }
        return i;
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {   
        void *ptr = NULL;
        int array[]={1,2,3,4,5,6,7,8,9,0};
        char *str;
        int totalBytes;
    
        ptr = (char *)malloc(sizeof(int)*3);
        str = (char *)malloc(10);
    
        totalBytes = getArraySize(ptr);
        printf("ptr = total bytes = %d and allocated count = %d\n",totalBytes,(totalBytes/sizeof(int)));
    
        totalBytes = getArraySize(array);
        printf("array = total bytes = %d and allocated count = %d\n",totalBytes,(totalBytes/sizeof(int)));
    
        totalBytes = getArraySize(str);
        printf("str = total bytes = %d and allocated count = %d\n",totalBytes,(totalBytes/sizeof(char)));
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-04 19:00

    In C you can't because array decays into a pointer(to the first element) when passed to a function.

    However in C++ you can use Template Argument Deduction to achieve the same.

    0 讨论(0)
  • 2020-12-04 19:04

    What you ask for simply can't be done.

    At run time, the only information made available to the program about an array is the address of its first element. Even the size of the elements is only inferred from the type context in which the array is used.

    0 讨论(0)
  • 2020-12-04 19:10

    You cannot calculate the size of an array when all you've got is a 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.

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

    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.

    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.

    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.

    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;
    }
    

    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++).

    0 讨论(0)
  • 2020-12-04 19:16

    You need to either pass the length as an additional parameter (like strncpy does) or zero-terminate the array (like strcpy does).

    Small variations of these techniques exist, like bundling the length with the pointer in its own class, or using a different marker for the length of the array, but these are basically your only choices.

    0 讨论(0)
  • 2020-12-04 19:18

    Size of an arry in C is :

    int a[]={10,2,22,31,1,2,44,21,5,8};
    
    printf("Size : %d",sizeof(a)/sizeof(int));
    
    0 讨论(0)
提交回复
热议问题