C++ count an array?

This is going to seem like a stupid question but how can I count the elements of a const array. Surely c++ compilers provide a count for const arrays?

1
2
3
const wchar_t* ItemHandler::itemNames[] = {L"Coins", L"Matches"};
const wchar_t* ItemHandler::itemIconLocations[] = {L"./media/Items/coins.png" , L"./media/Items/matches.png"};
const bool ItemHandler::itemStackable[] = {1, 1};


I want to count the elements so if the server sends a bad item id it won't crash every client in range lol. I heard that the sizeof keyword returns the size of the array in bytes. I used to think the sizeof keyword would return the element count but found out it isn't.
Last edited on
Using sizeof with class member arrays does give the length. sizeof(array) / sizeof(*array).
It only doesn't work if the class is a variable length struct and the array is the variable length part.
Topic archived. No new replies allowed.