-3

I defined the following function to find the length of an integer array:

int loa(int arr[]){

    int size = sizeof(*arr)/sizeof(arr[0]); 
    return size; 

}

However, when I do the following:

int main(){
int arr[3] = {1, 2, 3}; 
    printf("length of arr is %lu\n", sizeof(arr)/sizeof(arr[0]));
    printf("length of arr is %d", loa(arr)); 
return 0;  
}

The output is:

length of arr is 3
length of arr is 1

I don't understand this behavior. What am I doing wrong here?

bad_chemist
  • 283
  • 1
  • 7
  • 2
    Here `int loa(int arr[]){`, `arr` is no longer an array, but a pointer to an array. The notation is confusing a little bit. – Damien Mar 29 '21 at 13:49
  • yes, so i dereferenced it when I did sizeof(*arr), right? – bad_chemist Mar 29 '21 at 13:51
  • you write `int size = sizeof(*arr)/sizeof(arr[0]);` instead of `int size = sizeof(arr)/sizeof(arr[0]);` you have added an * – Zeuzif Mar 29 '21 at 13:51
  • *arr is the first element of your array its equivalent to arr[0] – Zeuzif Mar 29 '21 at 13:52
  • `sizeof(arr)` is the size of a `int *`, whatever the size of the original array itself. And as noted above, `sizeof(*arr)` is the size of the first element. – Damien Mar 29 '21 at 13:53
  • why have i been dowvoted...? I apologize if this was a bad question. – bad_chemist Mar 29 '21 at 13:53
  • `sizeof` is resolved into a constant value at compile time, you were trying to get the size of the array that was passed into your function at runtime. There is no way to reformulate the expression to do that. –  Mar 29 '21 at 13:55
  • You are probably asking this https://stackoverflow.com/questions/5493281/c-sizeof-a-passed-array. And probably you meant `sizeof(arr)/sizeof(arr[0])` instead of `sizeof(*arr)/sizeof(arr[0])` also in `loa`, which won't yield the result you expect either. – Jabberwocky Mar 29 '21 at 13:56
  • 1
    So you are aware that `sizeof(*arr)/sizeof(arr[0])` is always 1, `*arr` being the exact same thing as `arr[0]`. – Jabberwocky Mar 29 '21 at 13:57
  • @Jabberwocky, I was not aware of that. I thought *arr meant I am dereferencing an entire array in C, not just the first element. – bad_chemist Mar 29 '21 at 13:59
  • @user207526 there is no such thing as _"dereferencing an entire array"_. `*arr` and `arr[0]` are two notations for the exact same thing. More generally `*(arr + i)` is equivalent to `arr[i]`. – Jabberwocky Mar 29 '21 at 14:01

1 Answers1

2

Following three forms of function signature are exactly equivalent and pass argument a single pointer:

int loa(int arr[]);

int loa(int* arr);

int loa(int arr[15]);

If you try to uses sizeof(arr) with the argument, you will always get sizeof(int*) as result.

If you want to pass an array to function in C, you should either wrap it in the struct (if the size of the array is fixed at compile time), or pass two arguments - pointer to the first element and the size.

Note:

There is also a typo in your code, where you get sizeof(*arr). This would be wrong even when arr is not an argument to the function, but an array declared locally, like int arr[15]. You need to use sizeof(arr) instead, but like I said above, this would not have helped you for function arguments.

SergeyA
  • 61,605
  • 5
  • 78
  • 137