-5

I have searched all the way through net but could not find a solution.

I want to find length of an int array (number of elements in the array).

Well I am trying to pass array in the function.

I tried

void myFunction(int array[])
{
   int size = sizeof(array)/sizeof(array[0]);
}

but no luck!

Here is the sample code: Sample Code

could any one help me in this?

Burhanuddin Sunelwala
  • 5,318
  • 3
  • 25
  • 51
  • What is wrong with what you've written? – Nawaz Apr 14 '13 at 17:06
  • That code works. What's your question? –  Apr 14 '13 at 17:06
  • it returns 1 always. basically sizeof(array) and sizeof(array[0]) both gives value 4 – Burhanuddin Sunelwala Apr 14 '13 at 17:07
  • 1
    Then your code is different from what's in the question (e.g. `array` is actually a pointer). Please edit. –  Apr 14 '13 at 17:08
  • If you're trying to figure out the size by passing `array` to a function it won't work. The array gets implicitly converted to a pointer to the beginning of the array, and the result `1` is that of dividing the size of an `int *` by size of an `int`. This is why posting an [SSCCE](http://sscce.org/) is important! – Praetorian Apr 14 '13 at 17:08
  • [Look at this](http://liveworkspace.org/code/vufqr$52), it returns 11 as expected. Please update your code so we can see *exactly* what you're doing. – David G Apr 14 '13 at 17:09
  • possible duplicate of [Why sizeof(param\_array) is the size of pointer?](http://stackoverflow.com/questions/11622146/why-sizeofparam-array-is-the-size-of-pointer). Also read this: [How do I use arrays in C++?](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c) – Praetorian Apr 14 '13 at 17:13
  • 1
    http://liveworkspace.org/code/vufqr$55 – Burhanuddin Sunelwala Apr 14 '13 at 17:15
  • For the record, you want to look at the "Why sizeof(param_array) is the size of pointer?" duplicate first. The others were guesses and don't apply directly to your code. –  Apr 14 '13 at 17:30

1 Answers1

1

When arrays are passed into functions, they undergo array-to-pointer conversion. This means an array of type T[N] will simply decay into T*. Notice how the size information has been lost. All that remains is a pointer to the first element of the array. Here is a quote from the Standard:

4.2 Array-to-pointer conversion

An lvalue or rvalue of type “array of N T” or “array of unknown bound of T” can be converted to a prvalue of type “pointer to T”. The result is a pointer to the first element of the array.

In short, sizeof( array ) returns the size of the pointer, not of the array. That's where the miscalculation derives. A simple solution would be to utilize dynamic containers like std::vector which contains its size information internally:

#include <vector>

void myFunction(std::vector<int> const &array)
{
    int size = array.size();
}

If using std::vector isn't an option, you can use a reference to an array instead while using a template argument to deduce the length:

template <unsigned N>
void myFunction(int (&array)[N])
{
    int size = N;
}
David G
  • 94,763
  • 41
  • 167
  • 253
  • so is there any way to find the length? – Burhanuddin Sunelwala Apr 14 '13 at 17:18
  • no actually that is a standard method signature! The array is passed in this way only void myFunction(int array[]). I cannot change the method signature! – Burhanuddin Sunelwala Apr 14 '13 at 17:21
  • @BurhanuddinSunelwala There's no option but to make some change to the function signature. You can pass the size as an extra parameter, or use a function template to deduce the size of the array, or use some container such as `vector`. Please read the questions I linked to in the comments. – Praetorian Apr 14 '13 at 17:29
  • @BurhanuddinSunelwala: After the array has *decayed* into a pointer, the size information is lost. Either the caller passes the size as a different element, or you change the function signature in some other way (pass a `vector`, or pass the array by reference/pointer) – David Rodríguez - dribeas Apr 14 '13 at 17:30
  • @BurhanuddinSunelwala See the edit. You can use templates to get the length. – David G Apr 14 '13 at 17:34
  • @0x499602D2 I appreciate your responses, but this as well doesn't work out for me! I am submitting this code online, and there is a standard method signature, which I cannot change! :) I don't know why people downvoted this question? – Burhanuddin Sunelwala Apr 14 '13 at 18:33
  • @BurhanuddinSunelwala There is no other way to find the length other than what has been suggested in this thread. You can't keep the method signature in this context. – David G Apr 14 '13 at 18:40