# Thread: size of array - why function gives size ONE only

1. ## size of array - why function gives size ONE only

the user-defined function size_of_array(numA) is meant to give the size of numA which is 5 but it gives 1... I wonder where went wrong? can anyone help pls...
insert
Code:
```#include <iostream>
using namespace std;
int size_of_array(int numA[]);

int main()
{
int numA[]={0,1,2,3,4};
int i=0;
int total=0;

for(i=0; i<size_of_array(numA); i++)
{
total+=numA[i];
}
float average=total/i;
cout << "average is "<<average;

return 0;
}
int size_of_array(int numA[])
{
int num_of_elements=sizeof(numA)/sizeof(int);
return num_of_elements;
}``` 2. This has been answered elsewhere. 3. Obviously, sizeof(numA) is 5, and sizeof(int) is 4(depending on the compiler). You have made 5/4 and that results in 1. 4. Originally Posted by Dawnson Obviously, sizeof(numA) is 5, and sizeof(int) is 4(depending on the compiler). You have made 5/4 and that results in 1.
Wrong. sizeof(numA) is 4 here. Had numA been an array rather than a pointer, of 5 elements, sizeof(numA) would have been 20.
Yes, depending on the compiler. 5. Originally Posted by Dawnson Obviously, sizeof(numA) is 5, and sizeof(int) is 4(depending on the compiler). You have made 5/4 and that results in 1.
Absolutely wrong. If num[] would have contained 8 elements then according to you the answer should have been 2 which is incorrect. The correct reason is in the link given by laserlight. When you pass an array to a function it decomposes into pointer to the the first element and thus sizeof(num)==4 which is equal to sizeof(int). 6. You could do this instead.
Code:
```#include <iostream>
using namespace std;

template<typename T, int N>
char (&array(T (&)[N]))[N];

int main()
{
int numA[]={0,1,2,3,4};
int i=0;
int total=0;

for(i=0; i<sizeof(array(numA)); i++)
{
total+=numA[i];
}
float average=total/i;
cout << "average is "<<average;

return 0;
}``` 7. Or how about using some typedefs? I am having trouble discerning the types that goes into that function. It's pure luck I can see it's a function, too.
Code:
```template<typename T, int N>
void myfunction(T (& name)[N])
{
//...
}```
But I also believe something like this works, where N is the number of elements. You could do a sizeof on name and get the real size in this case.
Or you could just use a vector and use the .size() method and multiple with the size of one element. 8. I think we need to wait til C++0x in order to really simplify that ugly function signature
Code:
```template<typename T, int N>
typedef T (&array_t)[N];```
** I assume that this will eventually be legal, although even Comeau with C++0x extensions doesn't recognise it right now, so perhaps I misunderstand what they actually mean by "adding support for template typedefs" :-)

Yes I would prefer an STL container too; Personally I only find myself using these to initialise containers using const arrays as lookup tables.

The reason I prefer the sizeof(char(&)[N]) approach is that the return type can be evaluated at compile time without ever actually calling the function, but I suppose a solution which actually calls a function would be OK aswell.
Code:
```template<typename T, int N>
int array_size( T (&dummy_arg)[N] )
{
return N;
}```
Or, if a range is needed for an STL container or algorithm then maybe returning a pointer would be better
Code:
```template<typename T, int N>
T* end_of( T (&array)[N] )
{
return array+N;
}``` Popular pages Recent additions 