40

i would like to find the size of integer array passed as an argument to a function. Here is my code

void getArraySize(int arr[]){
  int len = (sizeof(arr)/sizeof(arr[0])
  printf("Array Length:%d\n",len);
}

int main(){
 int array[] = {1,2,3,4,5};
 getArraySize(array);
 return 0;
}

I am getting the following warning

sizeof on array function parameter will return size of 'int *' instead of 'int []' [-Wsizeof-array-argument]

Please help so that i can find the length of integer array inside the function getArraySize However am able to find the size of the array inside the main function.Since it is passed as a pointer, am not able to find the length from with in the function.

i do have an idea though.I could put this with in a try/catch block(C does not have try catch,Only jumpers which is OS dependent) and loop until i get a segmentation fault.

Is there any other way i could use to find the length of integer array inside the function

Community
  • 1
  • 1
Sunil Hari
  • 1,716
  • 1
  • 12
  • 20

4 Answers4

79

You cannot do it that way. When you pass an array to a function, it decays into a pointer to the first element, at which point knowledge of its size is lost.

If you want to know the size of an array passed to the function, you need to work it out before decay and pass that information with the array, something like:

void function (size_t sz, int *arr) { ... }
:
{
    int x[20];
    function (sizeof(x)/sizeof(*x), x);
}
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 3
    So you always have to pass it as an argument ??@paxdiablo – Sunil Hari Sep 05 '14 at 06:53
  • @SunilHari Yes `arr` in main function is not same as in getArraySize! In function `getArraySize` declaration, void `int arr[]` is same as `int * arr` and `arr` is a pointer but *not* an array. -- that is the message of compiler you are getting. – Grijesh Chauhan Sep 05 '14 at 06:54
  • @Sunil: yes, you need to evaluate the size in the scope where you still have access to it as an array, not after it's decayed. – paxdiablo Sep 05 '14 at 06:55
  • @paxdiablo:So i tried to do something which is not possible in c. :).Is putting jumpers a good approach? – Sunil Hari Sep 05 '14 at 06:56
  • 1
    I upvoted this answer because it helped me solve the issue at hand. However had I have to admit that I was shocked to see that this is not easily possible in C - I will never be able to understand why C became so popular as a language, excluding of course the obvious answer that it leads to fast programs. But from a usability point of view, having to do so many poor man workarounds is quite ... shocking. – shevy Mar 12 '20 at 12:30
16

When you pass an array to a function, its address is passed to the function as a pointer.

So void getArraySize(int arr[]) is equivalent to void getArraySize(int* arr).

As a result, sizeof(arr) yields sizeof(int*), which is probably not what you want.

You can safely use sizeof(arr)/sizeof(arr[0]) only within the scope of declaration of arr.

In your case, the scope of declaration of arr is function main.

barak manos
  • 29,648
  • 10
  • 62
  • 114
12

The array decays to a pointer when passed.

So sizeof only works to find the length of the array if you apply it to the original array.

So you need to pass length of array as separate argument to function.

Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
11

There is no way to determine the length inside the function. However you pass arr, sizeof(arr) will always return the pointer size. So the best way is to pass the number of elements as a separate argument.

sizeof only works to find the length of the array if you apply it to the original array.

int arr[5]; //real array. NOT a pointer
sizeof(arr); // :)

However, by the time the array decays into a pointer, sizeof will give the size of the pointer and not of the array.

void getArraySize(int arr[]){
sizeof(arr); // will give the pointer size
}

There is some reasoning as to why this would take place. How could we make things so that a C array also knows its length?

A first idea would be not having arrays decaying into pointers when they are passed to a function and continuing to keep the array length in the type system.

When you pass an array to a function it decays to pointer. So the sizeof function will return the size of int *. This is the warning that your compiler complining about-

sizeof on array function parameter will return size of 'int *' instead of 'int []'

As i suggested when you pass the array to the function you need to pass the Number of elements also-

getArraySize(array, 5); // 5 is the number of elements in array

Catch it by-

void getArraySize(int arr[], int element){
// your stuff
}

Else general way to calculate array size-

void getArraySize(int arr[], int len){
  // your stuff
  printf("Array Length:%d\n",len);
}

int main(){
 int array[] = {1,2,3,4,5};
 int len = (sizeof(arr)/sizeof(arr[0]); // will return the size of array
 getArraySize(array, len);
 return 0;
}
Sathish
  • 3,740
  • 1
  • 17
  • 28