Since it is tougher to demonstrate this with pointers to objects, I've subbed in the common integer in place of the pointer to MyStructure. The levels of indirection are unchanged, and it is the level of indirection that matters to the OP's problem.
By the way, do not do this. Use Ediac's solution. I'm only trying to point out where things went wrong for the OP. Walking through a 2D array in one dimension MAY work. And it may not. Have fun debugging that! This is only working because it's easy to implement a 2D array as a 1D array, but to my knowledge this behaviour is not guaranteed. It is certainly not guaranteed with a vector or other conventional dynamic array solution. Please slap me down if I'm wrong.
#include <iostream>
using namespace std;
//begin function @ Seraph: Agreed. Lol.
int main()
{
// ordering the array backwards to make the problem stand out better.
// also made the array smaller for an easier demo
int myStructure[4][4] = {{16,15,14,13},{12,11,10,9},{8,7,6,5}, {4,3,2,1}};
int i = 0;
// here we take the contents of the first element of the array
for (int s = myStructure[0][0]; i < 16; i++, s++)
{ //watch what happens as we increment it.
cout << s << " ";
}
cout << endl;
// output: 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
// this didn't iterate through anything. It incremented a copy of the first value
// reset and try again
i = 0;
// this time we take an extra level of indirection
for (int * s = &myStructure[0][0]; i < 16; i++, s++)
{
// and output the value pointed at
cout << *s << " ";
}
cout << endl;
// output: 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
// now we have the desired behaviour.
} //end function end Lol
output:
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Type* variable. Always useType *variable. Mind the space. The * is part of the variable, not the type. Consider,int *foo, goovs.int* foo, goo.Type* variablesyntaxsis not an address ofmyStructure[0][0](which would be of typeMyStructure**) - it's the value ofmyStructure[0][0].s++moves to the next element in the array to whichmyStructure[0][0]points (and if it doesn't point to an array, then your program exhibits undefined behavior). In any case, the value ofs++bears no relation tomyStructure[0][1]normyStructure[1][0]