how to iterate through an array without knowing the size in c

Viewed 6235

I have a pointer to an array in C which I would like to iterate through, but I don't know the size:

int *array;

I am unsure as to how I should proceed. I was thinking that I should probably try by finding the size by doing:

int array_size = sizeof(array) / sizeof(int);

But I don't know if that could work. I was wondering if there was a more optimal way of doing this?

4 Answers

In C, there is no way to tell the number of elements in an array from a pointer to an element. sizeof(array) / sizeof(*array) only works for an actual array, not for a pointer, which is what a function receives as an argument, even if the array syntax is used in the function prototype. In this case sizeof(array) evaluates to the size of the pointer, so dividing that by the size of an element is meaningless.

The best approach for your function to get the number of elements is to provide it as a separate argument.

If this is not practical, there are different ways to infer the number of elements, relying on a convention that must be adhered to by the callers:

  • the array could have a known fixed number of elements

  • the array could have a sentinel value as the last element of the array.

    It is common to use a null pointer (NULL) as such a sentinel for arrays of pointers, such as the char *argv[] argument of the main() function, but note that int argc is also provided to this function.

    The null byte (\0) is used to tell the end of C strings, which are arrays of char.

    In your case, you could consider 0 or -1 to signify the end of the array, but this convention must be used consistently by all callers of your function.

You cannot iterate over an array in c without knowking the number of elements.

Please note that sizeof(array) / sizeof(array[0]) won't work on a pointer, i.e it will not give the number of elements in the array.

It will also not work inside a function, where the array was passed as an argument due to array decay to pointer.

If the array does not contain a known sentinel value (as for example character arrays that contain strings have as the sentinel value the terminating zero character '\0') then you can not find its end.

If you have a pointer like this

int *array;

then the expression sizeof( array ) will yield the size of the pointer itself that does not depend on whether the pointer points to a single object of the type int or to the first element of an integer array with a known number of elements in the array. That size for example can be equal either to 4 or 8 depending on the used system. Thus the expression sizeof( array ) / sizeof( int ) in general will always yield either 1 or 2.

So you have to pass to the function explicitly also the number of elements in the array.

You could determine the number of elements in an array if you pass a pointer to the whole array. For example

void f( int( *a )[10] );

//...

int a[10];

//...

f( &a );

In this case dereferencing the pointer within the function you will get an object of the array type. Thus the expression sizeof( *a ) will yield the size of the original array.

I have a pointer to an array in c to which I would like to iterate through but I don't know the size:

You are in luck if you truly have a pointer to an array as the type of a pointer to an array carries information about the array size.

  int some_array[7] = {1, 2, 3, 4, 5, 6, 7};
  int (*pointer_to_an_array)[7] = &some_array;

  #define N (sizeof(*pointer_to_an_array) / sizeof(*pointer_to_an_array[0]))
  for (size_t i = 0; i < N; i++) {
    printf("%d\n", (*pointer_to_an_array)[i]);
  }

  

Unfortunately, with int *array;, code does not have a pointer to an array, but a pointer to an int and information about the original array size of some_array[] is not available through array.

int some_array[7] = {1,2,3,4,5,6,7};
int *array = some_array; // Not a pointer to an array

Carry information about array size in another variable.

size_t some_array_n = sizeof some_array/ sizeof some_array[0];
Related