Do this with:
std::size(myarray);
std::size is in <iterator>
.
Some sources will tell you to use a "trick" like sizeof(myarray)/sizeof(myarray[0])
. But this is error-prone. The name of an array decays really easily to a pointer, for which this "trick" gives the wrong result. std::size
will either work, or break the build.
Plus, when you switch from C arrays to std::array
, it'll still work!