c++ - Find the size of a c array in function -


this question has answer here:

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

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -