size of an array in called function

How to find the sizeof an array in called function?
when we pass the array a argument to function definition we will be having base address of array, so my understanding is that we will not get the size of an array? but is there any hacking for this to find size of array other than passing as size an argument to this called function?
Pass by reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
 
template < typename T, std::size_t N > void foo( T(&array)[N] )
{
	std::cout << "size: " << sizeof(array)  
	          << "    num elements: " << N << '\n' ;
}
 
int main() 
{
	int a[23] ; foo(a) ;
	const double b[57] = {0} ; foo(b) ; 
}

http://ideone.com/h996CE
void mergeSort(int A[])
{

//* here I want to find number of elements in the array A

if (n < 2)
return; //BASE CASE
int mid;
mid= n / 2;

}
int main(){
int A[]={10,5,3,37,13,9,24,7,39,19};
mergeSort(A);
}

In this case how to find number of elements in the array?
I thought if we know size of array we can get the number of elements, but size of array can't be found in definition.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
 
template < std::size_t NUM_ELEMENTS > void mergeSort( int (&A)[NUM_ELEMENTS] )
{
    //* here I want to find number of elements in the array A
    std::cout << "number of elements in the array A is: " << NUM_ELEMENTS << '\n' ;
 
    // ...
 
}
 
int main()
{
    int A[]={10,5,3,37,13,9,24,7,39,19};
    mergeSort(A);
}

http://ideone.com/kzM5l3
Topic archived. No new replies allowed.