How do we get the number of elements in an arraywithout using sizeof. In any case sizeof does not get me the correct number of elemnts so I want to implement my self without using sizeof.
I tried to run using sizeof and it doesn't print the correct number of elements in an array.
int n = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < n; i++){
printf("%s\n", arr[i]);
}
// to check if the noof elts in the arr is correct
printf("%d\n", n);
When you use an array as an argument in a function call, it is automatically converted to a pointer to its first element. The C standard does not provide any means for finding the size of an array given only a pointer to its first element.
For a function to know the size of an array that is passed to it in this way, you must pass the size as a separate argument or must provide the function some other way of knowing the size, such as including a sentinel value to mark the end. (For example, the null character that marks the end of a string is a sentinel.)