Pointer arithmetic and dereferencing

In the following code, can someone explain to me what the line in bold does.

  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;
 }
+4
source share
5 answers

Think of it as

cartoon.kenny[1] = cartoon.stan; 

They are basically the same

+2
source

If we translate everything into one common style of using the subscript operator [] (possibly with & ) instead of a combination of * and + , it will look like this

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

After

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

you can think of kenny as an β€œarray” of int * elements embedded in the kyle array with 2 element offsets: kenny[0] equivalent to kyle[2] , kenny[1] equivalent to kyle[3] , kenny[2] equivalent to kyle[4] etc.

So when we do

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

this is equivalent to doing

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

This is basically what the last line does.

In other words, if we exclude kenny from consideration ("kill Kenny"), assuming that the rest of the code (if any) is independent of it, all your code will be equivalent

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

As for all this ... I have no idea.

+2
source

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

cartoon.stan[1] = 4; sets the second element of the stan (int) array to 1.
cartoon.kyle[0] = cartoon.stan + 1; sets the first element of the kyle array (pointer to int) to point to the second element of the stan array (which we just set to 4).
cartoon.kenny = &cartoon.kyle[2]; sets the kenny pointer to point to the third element of the kyle array.
*(cartoon.kenny + 1) = cartoon.stan; sets the fourth element of the kyle array (pointer to int) to point to the first element of the stan array (which has not yet been initialized). More details:

cartoon.kenny gets the address of the kenny pointer (the third element of the kyle array),
cartoon.kenny+1 gets the next int after this address (the fourth element of the kyle array, which is a pointer to int),
*(cartoon.kenny + 1) dereferences this pointer, so we can set it, and = cartoon.stan indicates that it points to the first element of the stan array.

+1
source

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

0
source

He sets up the last element of Kyle, pointing to the first element of Stan.

0
source

Source: https://habr.com/ru/post/1313696/


All Articles