This cannot be done with pointers only. Pointers do not contain information about the size of the array - they are only a memory address. Since arrays break up into pointers when passing a function, you lose the size of the array.
One way is to use templates:
template <typename T, size_t N> size_t foo(const T (&buffer)[N]) { cout << "size: " << N << endl; return N; }
Then you can call a function like this (like any other function):
int main() { char a[42]; int b[100]; short c[77]; foo(a); foo(b); foo(c); }
Output:
size: 42 size: 100 size: 77
source share