Create a function to return the length of an int array in C

Viewed 576

I'm new to c programming. I want to create a function that takes an integer array as argument and returns length of the array. I know that the code below calculates the length correctly.

    int arr[] = {1, 2, 3, 4, 5};
    int length = sizeof(arr) / sizeof(arr[0]);

But if I create a function like below and pass the array as an argument, it doesn't work.

    int length_of(int* arr) {
        return sizeof(arr) / sizeof(arr[0]);
    }

My guess is that I'm not passing the array into the function correctly. What is the correct way of implementing this?

7 Answers

Well, there's no way of getting this done from a function. Any array, passed to a function as argument will decay to a pointer type (to the first element). So, inside the called function, all you will get is a size of a pointer.

An indirect way of achieving this would be to have something called a sentinel value in the array, and from the called function, iterate over the array elements to find the sentinel value while incrementing a counter, and once found, take the length of that counter.

Here's what the standard says (C99 6.3.2.1/3 - Other operands - Lvalues, arrays, and function designators):

Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue.

This means that pretty much anytime the array name is used in an expression, it is automatically converted to a pointer to the 1st item in the array.

You are passing a pointer as the parameter into your function, so it is not possible to know the size of the array by just knowing the size of parameter you pass to the function.

The most standard way of calculating size of an array in C is the one you mentioned in your question:

int a[10];
size_t n = sizeof(a) / sizeof(a[0]);

As other answers have pointed out, it is not possible to determine the size of an array inside of a function in the general case, i.e. without assumptions.

If this is really about returning the size of an array from a function at any cost, I chip in the general method of determining the length based on special knowledge/assumptions on the CONTENT of the array. This is how the strlen() function determines the length of NULLTERMINATED character sequences, which is the closest thing C has to string.
The special assumption on the content of the character sequence is that they only contain the terminating '\0' at the end. Many a StackOverflow question was asked because of the traps and vulnerabilities involved in that concept causing misunderstandings.

So, if you can for example assume that the array in question only contains non-negative integers (i.e. positive values and 0) and that the last valid value inside the array, at index (size-1), is negative, then you can determine the length of the array by search for that terminator, i.e. the first negative value you find at or behind the address from the pointer parameter.

If your prime intention is to return the array size from the function then pass the size as the argument.

Because once the argument is passed to function it will be pointing to the first element

So the code goes like

#include <stdio.h>
int func_len(int number)
{
    return number;
}
int main()
{
    int arr[]={1,2,3,4,5};
    int size = sizeof(arr)/sizeof(arr[0]);
    printf("Length is %d\n",size);
    printf("Length is %d",func_len(size));
}

Output will be

Length is 5
Length is 5

Your passing int pointer not the complete array ;

4/4 = 1

so 1 will be returned from the function

Normally size of pointer is 4 & size of int in 32/64 bytes compiler is 4

Related