1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
int mat[4][3]= { {25, 23, 26}, // declares a 2D array and fills it
{19, 27, 23}, // mat[1][0] == 19, mat[0][2] == 26, etc
{6, 15, 35},
{18, 23,56} } ;
int * ptr; // declares a pointer.
ptr = mat[0]; // makes the pointer point to mat[0][0]. IE: points to start of matrix
cout << mat << endl; // prints the address of the matrix I'm surprised this compiles..
// but then again multidimensional array syntax doesn't make a lot of sense
cout<< *ptr << endl; // prints what our pointer points to. Since it points to mat[0][0], this prints "25"
cout << *(ptr + 9 )<< endl; // adds 9 to the pointer and prints what that points to.
// since ptr points to mat[0][0], this prints mat[0][9]
// which is the same as mat[3][0] because the matrix has a "width"
// of 3 (multidimensional arrays are stupid) prints: "18"
cout << * (ptr) - 9 << endl; // this prints what 'ptr' points to, minus 9. Since ptr points to mat[0][0], this
// is 25-9, which prints "16"
cout << *(ptr++) << endl; // this prints what 'ptr' points to, *THEN* increments the pointer to point
// to the next int. So this will print 25, and pointer will now point
// to mat[0][1]
cout << *ptr << endl; // prints what ptr points to. Since that is now mat[0][1], this prints "23"
cout << * ptr +11 + * ptr + 10 << endl; // *ptr is what 'ptr' points to (which is mat[0][1]=23)
// so this prints 23+11+23+10... "67"
cout << ++ (* ptr --) << '\t' <<ptr++ << endl; // see below
|
note the difference between prefix and postfix inc/dec operators
ptr++ and ++ptr do the same thing, but return different valeus. ++ptr returns the new value, ptr++ returns the old value
EDIT: after checking with your output it's not quite what I expected
*goes over explanation to see what he missed*
EDIT again: found my mistake. it's a bigger precedence nightmare than I originally thought. Will edit again with a fix in a bit. Stay tuned
cout << ++ (* ptr --) << '\t' <<ptr++ << endl;
This line is an operator precedence nightmare. Operators are evaluated highest precedent first left to right, then lower precedents left to right, and so on until all levels of precedent are covered.
We have several operators here. The (relative) precedence they have is indicated in brackets. higher numbers = higher precedence (evaluated first). Operators of same precedence are evaluated left to right.
() parenthesis [0]
-- postfix increment ptr-- [1]
++ postfix increment ptr++ [1]
++ prefix increment ++() [2]
* dereference *ptr [2]
<< insertion [3]
Now this is a perfect example of code you should never ever write ever. And it's not even really important to know the exact details here unless you're attempting to write a C/C++ compiler. I'm a little fuzzy on the exact order of operations myself, but based on the output, this is what's happening:
Given that 'ptr' points to mat[0][1]... the following is happening (in order):
1) second postfix operator is executing (ptr++). This returns &mat[0][1], *then* increments ptr to point to mat[0][2].
2) first postfix operator is executing (ptr--). This returns &mat[0][2], *then* decrements ptr to point to mat[0][1]
3) * is dereferencing what ptr-- returned.. giving you mat[0][2] (=26)
4) prefix increment increments mat[0][2], incrementing it to 27, and returning the new value (27)
5) << operators proceed to print what those above expressions returned which is "27" and the address of mat[0][1] (which is sizeof(int) greater than the address of mat[0][0] .. and since sizeof(int) is 4 in this case, that's why it's 4 higher than the first pointer printed)
*Why* step 2 isn't done first I still don't understand. Logically it should be first. Not only is it encased in parenthesis, but it's also first in left-right order. Perhaps it's a compiler error. Or perhaps the parenthesis are not to be evaluated until it's time to evaluate the prefix increment operator.