2

Possible Duplicate:
Calculating size of an array
Sizeof an array in the C programming language?

Why does sizeof(arr)/(double)sizeof(arr[0]) work and sizeof(arr+0)/(double)sizeof(arr[0]) not work?

And why does sizeof(arr)/(double)sizeof(arr[0]) also not work when arr is passed to a function? Here is the full code example:

#include <iostream>

using namespace std;

int givesize(int arr[])
{
   cout<<sizeof(arr)/(double)sizeof(arr[0])<<"\n";
}

int main()
{
   int arr[] = {1,2,3,4,5};
   cout<<sizeof(arr)/(double)sizeof(arr[0])<<"\n";
   cout<<sizeof(arr+2)/(double)sizeof(arr[0])<<"\n";
   givesize(arr);
   return 0;
}

Output 5 1 1

Community
  • 1
  • 1
neo434
  • 29
  • 2
  • 3
    See http://stackoverflow.com/questions/720077/calculating-size-of-an-array, and hundreds of others – Mat Jul 07 '12 at 15:36

4 Answers4

4

This happens because arrays decay to pointers when they are passed to functions that take pointers as arguments.

Inside your main, the

int arr[] = {1,2,3,4,5};

declaration is equivalent to

int arr[5] = {1,2,3,4,5};

because the compiler has enough information to calculate the 5 from the aggregate initializer. In the function header, however, int arr[] does not mean the same thing: there is no context around it to tell the compiler that it's anything but a pointer to an integer.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

int arr[] in a function argument list is the same as int* arr. Which means that in:

int givesize(int arr[])
{
   cout<<sizeof(arr)/(double)sizeof(arr[0])<<"\n";
}

sizeof(arr)/(double)sizeof(arr[0]) is actually sizeof(int*) / (double)sizeof(int). As it outputs 1 that implies a 32-bit platform.

int arr[] as a local variable is not the same as int* arr, hence sizeof(arr)/(double)sizeof(arr[0]) is the same as sizeof(int[5]) / (double)sizeof(int) which evaluates to 5.

Next, sizeof(arr+2)/(double)sizeof(arr[0]) is again the same as sizeof(int*) / (double)sizeof(int), because the type of expression arr+2 is int*.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
0

Inside main, arr has type "array of five ints", but arr+0 has type "pointer to int". When you add zero to arr, you are treating it like a pointer, and as soon as you use an array like a pointer, it gets converted to a pointer.

Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
0

Why does sizeof(arr)/(double)sizeof(arr[0]) work

Because sizeof(arr) returns the size of the entire array in bytes (let's say 20 assuming int is 32 bits), and sizeof(arr[0]) returns the size of the first int (which is 4 bytes). This will give us 5.

And why does sizeof(arr)/(double)sizeof(arr[0]) also not work when arr is passed to a function?

Because arrays decay into pointers. This means they lose an important piece of information: their size. We cannot directly calculate the size of an array just by a pointer. How would the function even know it is an array to begin with?


C++? Use a template function:

template <typename T, int N>
int getsize(const T (&arr)[N])
{
    return N;
}
Marlon
  • 19,924
  • 12
  • 70
  • 101