I can’t figure out how to found size of array here is what i did
#include "stdio.h"
#define DEBUG 1
static void print_array(int arr[]);
int main (int argc, const char * argv[])
{
int str_numbers[5] = {1,4,8,2,9};
print_array(str_numbers);
}
static void print_array(int arr[])
{
int total = sizeof(arr)/sizeof(arr[0]);
#ifdef DEBUG
printf("Total %d", total);
#endif
}
Result ot total is 2 instead 5. What I miss ?
Wild guess: you aren’t showing your code but you are in fact doing this for the parameter of a function in a 64 bit implementation with 32 bits int. sizeof(array) is the size of the array, sizeof(pointer) is the size of the pointer, not the size of the array to which the pointer may point.
gives
and it is what I expect on such implementation.
Edit: Using a syntax like
print_array(int arr[])doesn’t change the fact that arr is a pointer. There could be a way in C99 to say it is an array, but I don’t remember it and VLA have been made optional in C11, to put the standard in agreement with the practice.