So, given the following:
#include <stdio.h>
int * getarr();
int main(int argc, char* argv)
{
int * arr = getarr();
printf("%d", sizeof(arr));
}
int* getarr()
{
static int a[4] = {0,1,0,3};
return a;
}
How does one find the length of arr? arr[4] == 0, but so does arr[0] and arr[2].
If this were a char*, the answer would be iterate until '\0', but that does not seem to work here as '\0' == 0.
Addressing arr[5] can seems to consistently result in a value > 163 – 1 (the size of an int on my system), but that does not seem to be a reliable measure as it strikes me as simply an empty location in memory.
Is there a way to retrieve this value consistently? Or does it simply have to be passed in?
You cannot retreive the length of the array when you are in the
main()function. This information has been lost when theint[4]was converted to anint *returned bygetarr()