0

How can I calculate the number of elements in the array if I don't want to pass the number of elements as another argument say,

fun(a, sizeof(a)/sizeof(*a));

and receive as

int fun(int a[], int limit)
{
}
user3386109
  • 34,287
  • 7
  • 49
  • 68
tubby
  • 2,074
  • 3
  • 33
  • 55
  • 1
    You cannot do that, arrays decays to pointer when passed to a function – P0W Jun 29 '15 at 23:02
  • Please use the search feature, there are dozens of questions , with answers, asking exactly the same thing – M.M Jun 29 '15 at 23:29

3 Answers3

1

How can I calculate the number of elements in the array

You can't.

if I don't want to pass the number of elements as another argument

You must.

This is C, where there are no baby sitters or magic wands. If you need to keep track of a piece of information (like the length of an array), you have to do so yourself. This explicitness is the reason we use C, so we know exactly what's going on, without the compiler doing crazy things behind our backs.


You'll see macros like this:

#define ARRAY_LENGTH(x)    (sizeof(x) / sizeof(x[0]))

But be careful! This only works for arrays, where the size is known at compile time. It will not work for an arbitrary pointer.

The Linux kernel provides this macro in a safe way:

kernel.h:

#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))

compiler-gcc.h:

/* &a[0] degrades to a pointer: a different type from an array */
#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))

compiler.h:

#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
0

If you do this:

main()
{
  int a[]={1,2,3};
  fun(a);
}
fun(int a[])
{
}

You are not passing the array as copy. It is only a pointer pointing to the address where the first element is in memory.

But it will work:

#include <stdio.h>

int fun(int*, int);

int main(void)
{
    int a[] = {1,2,3,4,5,6,7};
    int si=sizeof(a)/sizeof(a[0]);
    int b=fun(a, si);
    return 0;
}

int fun(int ar[], int si)
{
    int i = 0;
    printf("size of a==%d\n",si);
    for(i=0; i<si; i++)
        printf("%d ",ar[i]);
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Mr. Perfectionist
  • 2,605
  • 2
  • 24
  • 35
0

The declaration

int fun(int someArray[])

is equivalent to

int fun(int *someArray)

which is to say that the only information that fun receives about the array is a pointer to the first element. So the only way to determine the size of the array (other than passing the size as an separate parameter) is to put a sentinel at the end of the array. A sentinel is a value that otherwise would not occur in the array.

For example, the following code uses the value -1 as a sentinel at the end of the array.

#include <stdio.h>

void fun(int a[])
{
    int i;
    for ( i=0; a[i] >= 0; i++ )
        printf("%d ", a[i]);
    printf( "\nArray has %d elements\n", i );
}

int main( void )
{
    int a[] = {1,2,3,4,5,6,7,-1};
    fun(a);
    return 0;
}

This is in fact how strings work in C. The NUL terminator '\0' acts as a sentinel at the end of the character array, so that the string length does not have to be passed as a separate parameter to all of the string functions.

user3386109
  • 34,287
  • 7
  • 49
  • 68