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...wonder where went wrong?

#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;
}
because anything divided by itself is 1 and I think that is whats happening. its pointing at the first data type in numA and dividing it by the size of int which would equal 1. I may be wrong though
I can second that - you're correct.
but why is it pointing to the first data type instead of all the data types
i dont know if you know about pointers yet
http://www.cs.stanford.edu/cslibrary/PointerFunCppBig.avi

Ok. In memory when you pass an array into a function it points to the first datatype. THen when you increment it goes to the next position etc. Its a little confusing. But that is just how it works
The compiler has no idea how many elements are present in the array when it is compiling the size_of_array() function.

It is likely taking the array pointer and giving you the size of that which just happens to be the same size as an int.

oh thanks a lot...
Topic archived. No new replies allowed.