Possible Duplicate: Calculating size of an array
This question has been asked before, but I want to confirm it. Let's say I have the following C++ function:
#include <stdio.h>
#include <stdin.h>
#define length(a) sizeof(a)/sizeof(a[0])
int main()
{
double c[] = {1.0, 2.0, 3.0};
printf("%d\n", getLength(c));
return 0;
}
int getLength(double c[]) { return length(c);}
This should return the wrong value because size of will return the size of the pointer as opposed to the size of the array being pointed at. This:
template<typename T, int size>
int length(T(&)[size]){return size;}
I know it works directly, but I want to know if I can somehow call it indirectly i.e. via a helper function. I understand that two possible alternatives are to either store the length of the array in a separate slot or use a vector, but what I want to know is:
Given a function getLength:
getLength(double arr[])
{
...
}
Is there a way to compute the length of arr
without passing any more information?