alculating number of elements in array is easy:
int myarr[] ={1,2,3};
int myarrsize = sizeof(myarr)/sizeof(myarr[0]);
but, how can I calculate number of rows in 2D array?
int my2Darr[][3]={{1,2,3},{4,5,6}};
int my3DarrRows = ???
Is it possible?
The same way. So either:
sizeof(my2Darr)/sizeof(my2Darr[0])
or:
sizeof(my2Darr[0])/sizeof(my2Darr[0][0])
depending on what you mean by "rows".
Your code won't compile unless you specify second array size which you know its size already:
int my2Darr[][3]={{1,2,3},{4,5,6}};
^^^
So the question come to how can I calculate number of rows in 2D array?
template <class T, unsigned N, unsigned N1>
int length(T (&)[N][N1])
{
return N;
}
If you want column size, return N1
instead
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With