开发者

Pointer arithmetic and dereferencing

In the following code, can anyone please explain to my what the line in bold is doing.

struct southParkRec {
    int stan[4];
    int *kyle[4];
    int **kenny;
    string cartman;
};

int main()
{
    southParkRec cartoon;
    cartoon.stan[1] = 4;
    cartoon.kyle[0开发者_开发知识库] = cartoon.stan + 1;
    cartoon.kenny = &cartoon.kyle[2];
    *(cartoon.kenny + 1) = cartoon.stan;  //What does this line do?

    return 0;
}


Think of it as

cartoon.kenny[1] = cartoon.stan;

They are basically the same thing


If we bring the whole thing to one common style of using the subscript operator [] (possibly with &) instead of a * and + combination, it will look as follows

cartoon.stan[1] = 4;
cartoon.kyle[0] = &cartoon.stan[1];
cartoon.kenny = &cartoon.kyle[2];
cartoon.kenny[1] = &cartoon.stan[0];

After the

cartoon.kenny = &cartoon.kyle[2];

you can think of kenny as an "array" of int * elements embedded into the kyle array with 2 element offset: kenny[0] is equivalent to kyle[2], kenny[1] is equivalent to kyle[3], kenny[2] is equivalent to kyle[4] and so on.

So, when we do

cartoon.kenny[1] = &cartoon.stan[0];

it is equivalent to doing

cartoon.kyle[3] = &cartoon.stan[0];

That's basically what that last line does.

In other words, if we eliminate kenny from the consideration ("kill Kenny"), assuming that the rest of the code (if any) doesn't depend on it, your entire code will be equivalent to

cartoon.stan[1] = 4;
cartoon.kyle[0] = &cartoon.stan[1];
cartoon.kyle[3] = &cartoon.stan[0];

As for what is the point of all this... I have no idea.


In cartoon you have:
- stan, an array of 4 ints.
- kyle, an array of 4 pointers to int.
- kenny, a pointer to a pointer to int, that is, let's say, a pointer to an "array of ints".

cartoon.stan[1] = 4; sets second element of stan array (an int) to 1.
cartoon.kyle[0] = cartoon.stan + 1; sets first element of kyle array (a pointer to int) to point to the second element of stan array (which we have just set to 4).
cartoon.kenny = &cartoon.kyle[2]; sets kenny pointer to point to the third element of kyle array.
*(cartoon.kenny + 1) = cartoon.stan; sets fourth element of kyle array (a pointer to int) to point to the first element of stan array (which hasn't been initialised yet). More in detail:

cartoon.kenny gets kenny pointer's address (third element of kyle array),
cartoon.kenny+1 gets the next int after that address (fourth element of kyle array, which happens to be a pointer to int),
*(cartoon.kenny + 1) dereferences that pointer, so we can set it, and
= cartoon.stan sets it to point to the first element of stan array.


It is incrementing the pointer at *cartoon.kenny. 'kenny' is a pointer to a pointer, so the first dereference returns a pointer, which is incremented, and a value assigned. So, *(kenny + 1) now points to the beginning of the array 'stan'.


It sets the last element of Kyle to point to the first element of Stan.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜