DarkLeader
DarkLeader

Reputation: 589

looping over an array in C

Say I want to loop over an array, so I used a basic for loop and accessed each element in it with the index but what happens if I don't know how long my array is?

#include <stdio.h>
#include <stdlib.h>

int main(){
    int some_array[] = {2,3,5,7,2,17,2,5};
    int i;

    for (i=0;i<8;i++){
        printf("%d\n",some_array[i]);
    }

    return 0;
}

This is just a simple example but if I don't know how big the array is, then how can I place a correct stopping argument in the loop?

In Python this is not needed since the StopIteration exception kicks in, but how can I implement it in C?

Upvotes: 1

Views: 2284

Answers (4)

Silerus
Silerus

Reputation: 36

In general, you can calculate the size of the array with:

sizeof(ArrayName)/sizeof(ArrayType)

but this does not work with dynamically created arrays

Upvotes: 1

Sami Shames El-Deen
Sami Shames El-Deen

Reputation: 94

You can use sizeof() to get the size of the array in bytes then divide the result by the size of the data type:

size_t n = sizeof(some_array)/sizeof(some_array[0]);

Upvotes: 2

giusti
giusti

Reputation: 3538

You have to know the size of the array. That's one of the most important rules of C programming. You, the programmer, are always responsible for knowing how large your array is. Sure, if you have a stack array or a static array, you can do this:

int array[size];
int size_of_array = sizeof array / sizeof *array;
for (int i = 0; i < size_of_array; i++) {
   // do something with each array[i]
}

But as you can see, you needed the variable size in the first place. So what's the point of trying to discover the size if you were forced to know it already?

And if you try to pass this array to any function

some_function(array); /

you have to pass the size of the array too, because once the array is no longer in the same function that declared it, there is no mechanism to find its size again (unless the contents of the array indicate the size somehow, such as storing the number of elements in array[0] or using a sentinel to let you count the number of elements).

void some_function(int *array) {
    /* Iterate over the elements until a sentinel is found.
     * In this example, the sentinel is a negative number.
     * Sentinels vary from application to application and
     * implicitly tell you the size of the array.
     */
    for (int i = 0; array[i] >= 0; i++) {
        // do something with array[i]
    }
}

And if it is a dynamically-allocated array, then you need to explicitly declare the number of elements anyway:

int size = 10;
int *array = malloc(sizeof *array * 10);

So, to summarize, you must always know the size of the array. There is no such thing in C as iterating over an array whose size you don't know.

Upvotes: 2

klutt
klutt

Reputation: 31379

Just do like this:

for (i=0; i<sizeof(some_array)/sizeof(some_array[0]); i++){
        printf("%d\n",some_array[i]);
}

But do beware. It will not work if you pass the array to a function. If you want to use it in a function, then write the function so that you also pass the size as argument. Like this:

void foo(int *arr, size_t size);

And call it like this:

foo(some_array, sizeof(some_array)/sizeof(some_array[0]));

But if you have a function that just take a pointer, there is absolutely no standard way to find out the size of it. You have to implement that yourself.

Upvotes: 2

Related Questions