-5

I have often seen c++ programs with a pointer to a pointer variable i.e **i. What does it mean and why is it used. Cant we just use a single pointer instead of that. What is the difference between a single pointer and a pointer to a pointer. Please explain each step. Thank you.

Rony
  • 1
  • 7
  • each step of what? and a pointer to a pointer is just a normal pointer. its a pointer that just happens to point to something pointing to a block of memory – DTSCode Feb 05 '16 at 17:30
  • 1
    what's the diff between a postit that says "socks are under the bed", and a post-it that says "directions to socks are on post-it on fridge"? – Marc B Feb 05 '16 at 17:31
  • Imagine an array. An array is a pointer to a bunch of items in a row. Now imagine a pointer to an array - that's a pointer to a pointer. – Andrew Williamson Feb 05 '16 at 17:32
  • a very common example is a 2D array or array of strings (char*) – Pooya Feb 05 '16 at 17:34
  • Possible duplicate of [How do pointer to pointers work in C?](http://stackoverflow.com/questions/897366/how-do-pointer-to-pointers-work-in-c) – surajs1n Feb 05 '16 at 18:21

1 Answers1

1

Variables take up some space to store. This space is taken from memory. Suppose your stack (memory) starts at 0x12 34 56 78and you have an integer a with value 4:

int a = 4;

Your memory might look like:

0x12 34 56 78: 0x00 00 00 04 (a)

Now suppose you also have a pointer to a:

int a = 4;
int* p = &a;

Your memory would then look like:

0x12 34 56 78: 0x00 00 00 04 (a)
0x12 34 56 7c: 0x12 34 56 78 (p)

Now suppose you have a pointer to p:

int a = 4;
int* p = &a;
int** q = &p;

Your memory would then look like:

0x12 34 56 78: 0x00 00 00 04 (a)
0x12 34 56 7c: 0x12 34 56 78 (p)
0x12 34 56 80: 0x12 34 56 7c (q)

You can get from q to p to a by following the addresses. Pointers are a layer of indirection: they specify where something is, not what it is.

alcedine
  • 909
  • 5
  • 18