Obtain the correct array size in a function

Hello Programmers,

I've been wondering for some days how can I obtain the length of an array that has been sent throughout a function. In the following code, I obtain "2" as output, while I was expecting "5".

1
2
3
4
5
6
7
8
9
10
void call(int a[])
{
	cout<<sizeof(a)/sizeof(int);
}

int main()
{
	int* a=new int[5];
	call(a);
}


Do you guys know how can I properly call this function so I can obtain the correct array size?

Cheers!
You can't. The array is not passed to the function, only a pointer is. And there is no way to find out how many elements a pointer points to.

You have to pass the size of the array as another parameter.

OR.. better yet... don't use arrays, and use a container class like a vector.
Mmmm that's disappointing.

Anyway, thanks for the clarification ;) !!!
Topic archived. No new replies allowed.