How can I get the length of an array in C, I tried it how i get it in other languages but it doesn't work like this:
int array [5];
for(int i = 0; i < array.length; i++) {..
How can I get the length of an array in C, I tried it how i get it in other languages but it doesn't work like this:
int array [5];
for(int i = 0; i < array.length; i++) {..
If its on the stack, you can use the sizeof
operator to determine its raw byte size:
int array[5];
size_t array_size = sizeof(array);
But this gives you the size in bytes, not the number of elements. However, you can calculate the number of elements with this approach:
int array[5];
size_t array_elems = sizeof(array) / sizeof(* array);
If you array is a pointer (e.g. function call or dynamic memory allocation) then you have to keep track it yourself!