Question: I'm still learning C++. Currently I'm learning arrays and want to get length of an array. I know to get length of an array I have to type:

```
int length = (sizeof(array)/sizeof(*array));

```

It works very well. But, when I make a method to get length that I named as getLength() it doesn't work. Here my code:

```
#include <iostream>
using namespace std;

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

int getLength(int arg[]){
    return(sizeof(arg)/sizeof(*arg));
}

int main(){
    //array length
    cout << "Displaying array length" << endl;
    cout << getLength(array) << endl;
    system("pause");
}

```

That should returns 5, but it returned 1. If I use previous way, it returns 5, even they have same code. 

Thanks for helps.

Answer: 
It's because `array` has *decayed* to a pointer type when it's passed to `getLength`.

So in that function, `sizeof` cannot be used to retrieve the total size of the array; it merely evaluates to the size of a pointer to the first element.

The normal thing to do in this instance is pass the size value in to `getLength` as a parameter.
When you call a function with an array, the array decays to the pointer to the first element. The array syntax is quite misleading. The two functions below are equivalent:

```
void func1(int* p) { /* do something */ }
void func2(int p[]) { /* do something */ }

```

See [this stack overflow answer](https://stackoverflow.com/questions/1461432/what-is-array-decaying#1461449) for details.

What you can do is to delve into templates. Here's a function that does work:

```
template <typename T, size_t N>
size_t array_length(T (&arr)[N])
{
  return N;
}

```

The template captures an array (`arr`) by reference, and the size (`N`) is known by the template matching. Your `main()` example would work with it.

With that said, though, I think you'll be far far better off using `std::vector<>` or `std::array<>` instead. With those, the size is trivially accessible and you get other pleasant properties, not least that they do not decay to pointers.