Whenever I iterate over a C++ array by pointer arithmetics (i.e. not with Java-style incrementing of an index), I first get the address of the last element in the array and then iterate as long as the current address is not the last address:
#include <iostream>
using namespace std;
int main()
{
int values[] = { 1, 2, 3, 4, 5 };
int* lastAddress = values + size(values);
for (int *p=values; p != lastAddress; p++) {
cout << "val: " << *p << endl;
}
}
Is this the generally accepted way of doing this kind of thing?