Monday, April 12, 2010

Pointer Puzzle



//----------------------
// Variable Definitions
//----------------------

int* pi;
int* pj;
int* pk;

int t;

double* pd;

//-----------------
// Some Statements
//-----------------

*pd += static_cast(*pi);

pi = &t;

*pi = *pk;

pj = pi;

*pj /= 3;

++pj;

++*pj;

//-----------------------------------------------------------------------------
// Assuming the following initial configuration of memory (on the left side),
// what is the resulting configuration of memory (on the right side) after the
// above statements execute?
//-----------------------------------------------------------------------------

                   BEFORE             AFTER

                 __________         __________
                |                        |       |                    |
       1100 |     9                 |       |          9        |
                |__________|       |__________|

                 __________         __________
                |                      |        |                      |
pi     1300|   1100           |        |    1350          |
                |__________|        |__________|

                 __________         __________
                |                       |       |                    |
t      1350 |    14              |       |       2            |
                |__________|       |__________|
                |                      |       |                     |
                |    20              |       |   21              |
                |__________|       |__________|

                 __________         __________
                |                      |        |                      |
pj     1380|   1100           |        |   1354           |
                |__________|       |__________|

                 __________         __________
                |                      |         |                      |
pk    1400|   1410           |         | 1410             |
                |__________|       |__________|

                 __________         __________
                |                      |        |                      |
       1410 |              7      |        |     7               |
                |__________|       |__________|

                 ____________________         ___________________
                |                                                |      |                                        |
       1430 |        0.0                                   |      |     9.0                              |
                |____________________|       |___________________|

                 __________         __________
                |                      |         |                      |
pd    1440|   1430           |         |1430              |
                |__________|        |__________|






1 comment:

Note: Only a member of this blog may post a comment.