1

If I have the following function in C++:

void functionName(HANDLE arr[100])
{
}

HANDLE hHandles[100];
functionName(hHandles);

Is there any way to know the size of 'arr' inside the functionName? (Without hardcoding it there)

ahmd0
  • 16,633
  • 33
  • 137
  • 233

4 Answers4

8

Use template to catch the N in T[N]

template <size_t N>
void function(HANDLE (&arr)[N]) 
{
  std::cout << N << " is here\n";
}
Joel Falcou
  • 6,247
  • 1
  • 17
  • 34
  • OK, yeah, this one uses templates though. – ahmd0 Feb 17 '12 at 04:51
  • you dont have many options anyway. The fact the size of an array is part of the array type in C++ is a very strong informations. Not using it result in subpar solution. – Joel Falcou Feb 17 '12 at 06:24
1

No. Unless you have put a delimiter at the end yourself before passing. In case of strings (which are character arrays)the delimiter is the null character '\0' so the size can be checked. But normally for arrays this can't be done.

vjain27
  • 3,514
  • 9
  • 41
  • 60
0

Consider sending the size as a separate parameter instead.

void functionName(HANDLE arr[100], size_t size)
{
}
Muhammad Hewedy
  • 29,102
  • 44
  • 127
  • 219
0

You could also use a global variable or a class attribute.

size_t size = 100;
void functionName(HANDLE* arr)
{
//size will be visible here and in any other
// functions and you can change its value during run-time

}
Mihai
  • 121
  • 3