Is this the generally accepted way of doing this kind of thing?
No, it isn't. You shouldn't do hand made pointer arithmetics for determining start and end of a container. That's what std::begin() and std::end() are for:
#include <iostream> using namespace std; int main() { int values[] = { 1, 2, 3, 4, 5 }; for (auto p = std::begin(values); p != std::end(values); p++) { cout << "val: " << *p << endl; } }
As a short form you can use a range based for loop, which uses the same mechanism under the hood:
for(auto value : values) { cout << "val: " << value << endl; };
Please note, that this only works with arrays declared locally with a well known size (sizeof()).
If you get such array definitions passed to a function, you still also need to pass the size:
foo(int values[], size_t size) { for(auto val = std::begin(values); p != std::begin(values) + size; ++p) { // ... } }
The standard accepted way is to ditch raw arrays at all, in favor of using std::array<T,N> for arrays of known size, or std::vector<T> for arrays of unknown size.
std::begin/std::enddon't return pointers. They return iterators.