As everyone knows this
Code:
atype array[4] = {1, 2, 3, 4};
atype *p = array;
for (int i = 0; i < 4; i++) {
OperateOn(*p);
p++;
}
is more efficient than this
Code:
atype array[4] = {1, 2, 3, 4};
for (int i = 0; i < 4; i++) {
OperateOn(array[i]);
}
(except for those of you who don't know, dereferencing an array can amount to a multiplication operation and an addition operation, while the first solution just does an increment)
But what if you have
Code:
atype array[4][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16} };
how do you go about iterating through that without using the square brackets? I tried
Code:
const atype **arrayy = (const atype **)array;
const atype *arrayx = *array;
But my first dereference of arrayx won me a seg fault. And arrayy was a sober-looking 0x7fffffffdb90 while arrayx was 0x200000001. No points for guessing where the "2" and the "1" of that come from. I just don't see how.