It is true that you cannot get the array size from a pointer to an element of the array.
If the reason you only have a pointer is because you are implementing a function that takes an array parameter as an argument like this:
void foo (T *p) {
// p is a pointer to T
}
then you can use a template function instead to get the array size to the function.
template
void foo (T (&p)[N]) {
// p is a reference to an array[N] of T
std::cout << "array has " << N << " elements" << std::endl;
std::cout << "array has "
<< sizeof(p)/sizeof(p[0])
<< " elements"
<< std::endl;
}
int main ()
{
int array[40];
char array2[25];
foo(array);
foo(array2);
return 0;
}