c++ - Find the size of a c array in function -
this question has answer here:
- why isn't size of array parameter same within main? 13 answers
- how find length of array? 18 answers
i trying write short function takes pointer array , returns it's size. far have this:
int main (void) { double myarray[3] = {0, 1, 2}; int temp = arraysize(myarray); return 0; } int arraysize(double * myarray) { return sizeof(myarray) / sizeof(*myarray); }
but doesnt seem working. appreciated, jack
that's impossible - pointer points @ first element of array. there's no way extract array size that.
you pass array reference, , infer size template parameter:
template <typename t, size_t n> size_t arraysize(t (&)[n]) {return n;}
this work if have access array (as in example). if it's decayed pointer, size information has been lost.
Comments
Post a Comment