0

Hi I have a question about double pointers. For example in this code:

int a, b=2;
int *iPtr1, **iPtr2;

iPtr1  = &a;
iPtr2  = &iPtr1;  
*iPtr1 = b+3;
*iPtr2 = iPtr1;

On the last line *iPtr2 = iPtr1; It that just telling iPtr1 to point back to itself since dereferencing a double pointer just once is like using iPtr1?

Vicky
  • 12,934
  • 4
  • 46
  • 54
MeesterMarcus
  • 700
  • 1
  • 9
  • 25
  • Others have given you the answer, but I'll just add that IMO it's hugely helpful to draw up a little map of your memory space and show what points to what. In this case you'd have squares of memory for a, b, iPtr1 and iPtr2. Initially a would be empty (uninitalised) and b would be 2. Then you'd draw an arrow from iPtr1 to a (first line of code - `iPtr1 = &a;`). Then you'd draw a line from iPtr2 to iPtr1 for the second line of code. Carry on in this way, using "*" to mean "follow the arrow and take what's there". – Vicky Feb 28 '13 at 15:51
  • Thanks for the tip! I definitely need to practice writing out and drawing like you say as I tend to just dive into the coding and it costs me later. – MeesterMarcus Mar 21 '13 at 15:19

2 Answers2

2

It makes *iPtr2 point to whatever iPtr1 points to. And as iPtr2 points to iPtr1 it's the same as iPtr1 = iPtr1.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Yeah that's what I figured. I guess my professor was just trying to trick us or maybe error. – MeesterMarcus Feb 28 '13 at 15:34
  • This question is a bit old but may I ask something? In the strtol function there's a char** endptr in the signature of the function, which can be used to point to the position in case the input string has an error and is not completely numerical. Now when checking it (e.g. in the example in the man pages) the check is like this: if(*endptr != 0){... isn't that wrong? (of course it's not) I'm asking because the endptr is declared as a pointer to a character pointer however it's just dereferenced once to point to the last character('\0') of the input string(which means success, or no error).why? – aderchox Nov 10 '18 at 13:17
  • I thought about it a bit more and I can guess the answer to my own question. the "\*\*" in the declaration of the endptr in the function signature is because we don't need the character pointer we pass to the function strtol to be "copied" rather we want to change its address and we want this address to be visible from "outside" the function as well and after it gets finished. so by "\*\*" we're passing a pointer by reference. I hope I'm right? – aderchox Nov 10 '18 at 14:20
  • 1
    @Narnia If the `strtol` function reaches the end of the ***null-terminated*** string, then the pointer `endptr` will be pointing to that null-terminator. And that null-terminator will be equal to `'\0'` which happens to be equal to the integer value `0`. Dereferencing `endptr` will then give you the value `0`. – Some programmer dude Nov 10 '18 at 23:40
  • 1
    @Narnia Also, the reason the argument is declared as a pointer to a pointer, is because it emulates *pass by reference* (which C doesn't have). You declare `endptr` as a normal pointer (i.e. `char *endptr`) then pass a pointer to the variable using the address-of operator `&`, as in `&endptr`. – Some programmer dude Nov 10 '18 at 23:41
1

Trace the execution with gdb, then you will see that the last line *iPtr2=iPtr1 doesn't change anything. (it's kind of like iPtr1=iPtr1)

On iPtr2 = &iPtr1;, the iPtr2 already points to the address where iPtr1 THE POINTER lies NOT THE ADDRESS iPtr1 points to.

Note: you cannot replace iPtr2=&iPtr1 with an *iPtr2=iPtr1, because at that point iPtr2 has garbage value (if it's a local non-static variable) and dereferencing it is undefined behaviour.

phoeagon
  • 2,080
  • 17
  • 20