2

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.

4
  • Also: stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c "5.3 Pitfall" in particular. Commented Dec 31, 2015 at 12:11
  • 1
    The answer to all your "how to use arrays in C++" is don't. Really. Use std::vector. Commented Dec 31, 2015 at 12:57
  • One way is to do it like std::end does it using a template non-type parameter to deduce the array size. Commented Dec 31, 2015 at 13:08
  • @ShafikYaghmour: That's not beginner-level C++, and besides a beginner should learn to use std::end(arr) - std::begin(arr). See also stackoverflow.com/a/26947706/15416 Commented Dec 31, 2015 at 14:12

3 Answers 3

1

Because when you pass the array to a function, the array decays to a pointer. So sizeof(arg) will return the size of the pointer on your architecture. If you need the size you will need to pass an extra parameter to the function. Check: What is array decaying?

Sign up to request clarification or add additional context in comments.

1 Comment

so I can't use a function to get length. Thank you for your explanation :). I'll continue my study.
1

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.

Comments

0

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 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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.