3

For example why would one need char **myVariable;? If a pointer is just an address in memory why what difference does it make if it's a pointer to an address with a pointer to an address of a char than just a pointer to an address with a char?

In assembly wouldn't this be like

LDR R3, =myVariable
LDR R2, =[R3]
LDR R1, =[R2]
LDR R0, =[R1]

Where a single pointer would be

LDR R1, =myVariable
LDR R0, =[R1]

now R0 holds the value? Obviously this way is faster.

Koushik Shetty
  • 2,146
  • 4
  • 20
  • 31
Celeritas
  • 14,489
  • 36
  • 113
  • 194

7 Answers7

11

In case you want to modify the value of pointer by a function, you will have to pass the address of that pointer to function by reference. Therefore you will need pointer to a pointer as argument.

Example :

int main()
{
  int num=0;
  int *p = #
  // This function passes the pointer by value. 
  //So when function returns, *p points to same place
  fn(p);

  // This function will actually change where the pointer points to as 
  // it was passed by reference
  fn2(&p);
}
void fn(int *ptr)
{
    static int i=1;
    ptr = &i;
}
void fn2(int **ptr)
{
    static int j=1;
    *ptr = &j;
}
Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
someone_ smiley
  • 1,006
  • 3
  • 23
  • 42
  • 1
    +1 this is an actual real-life example, one of the correct answers. –  May 14 '13 at 05:10
  • 5
    Though a reference would be better for this purpose, since we're talking about C++. :P – cHao May 14 '13 at 05:13
  • I would point out that you can usually use a reference to a pointer to handle this case. The exception being when you want the option of easily passing NULL to indicate you *don't* care about that output value. – Mike Woolf May 14 '13 at 05:14
10

Well, if you have a table of pointers, a pointer to that table would have the type "pointer to pointer".

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
3
int main ( int argc, char **argv )

an array of strings.

of course it is slower, you have the extra step in the middle, but without that extra step you cant have an array/table/list of pointers. You just have one.

old_timer
  • 69,149
  • 8
  • 89
  • 168
2

Replies above have address the first part of your question. Answer to second part "what difference does it make if it's a pointer to an address with a pointer to an address of a char than just a pointer to an address with a char?"

Though in terms of memory layout, what you are saying is right, C/C++ handles the pointer depending on its type. When you do pointer++, it increments the pointer by size of the data-type it is pointing to. if it is a pointer to an integer it will be incremented by 4, if it is a pointer to character it will be incremented by 1, if it is a pointer to a structure of size 20, it will be incremented by 20.

Jack
  • 741
  • 1
  • 8
  • 25
1

Pointer to a pointer is required when you want deal with storing pointer and dealing with it; e.g. to change the content of the underlying pointer. For example:

int i = 0, j = 1;
int* p = &i;
int** pp = &p;

Now you want to make p point to j instead of i, then you can do as:

*pp = &j;  // equivalent to `p = &j;`

This is just for explanation. In real world this is required when you are dealing with functions.

void Destroy (int** pp) {  // for any int pointer
  delete[] pp;  // deallocate the memory
  *pp = 0;  // set the pointer to 0, so that it's not dangling
}

And use it as:

int* pi = new int[30];
...
Destroy(&pi); // `pi` is now pointing to 0

But still in C++, you have superior alternative as "pointer reference". Which does mostly the same thing, but with better readability.

void Destroy (int*& p) {  // for any `int` pointer 
  delete[] p;  // destroy the memory
  p = 0;  // Null out the same pointer which was actually passed
}

use as:

Destroy(pi);  // no need to pass address
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • 1
    Pass by reference is easier to call and adds some level of protection. However, since pass by reference is almost inevitably implemented as pass by pointer, pointer reference and pointer to a pointer are pretty much the same thing performance-wise. – David Hammen May 14 '13 at 05:33
0
  1. Let's say you have a pointer and the value of the pointer matters. For example the pointer points to an element in an array. Then you call a function with that pointer, and you mean it as an input/output parameter, so the function would modify it. In this case this is possible by putting one more indirection into the game and passing the pointer of the pointer to the function, so it can change the pointer.
  2. @Lindyracer's: array of pointers.
  3. in some OSes if you get hold of a "global" pointer, then it uses a double indirection (usually you get it as a type defined by a typedef). The reason behind this is that if you store the double pointer, then the memory manager is free to change the pointer pointed by that double pointer, so it can move around things, and defrag, etc.

There are many more.

Csaba Toth
  • 10,021
  • 5
  • 75
  • 121
0

First, because it's semantically possible and necessary to complete the language, leaving no undefined territory as to what does this expression mean, &p where p is a pointer?

And it's also quite handy as it seems. Here's an answer from Linus Torvalds. Using pointers to remove item from singly-linked list

Community
  • 1
  • 1
xiaofeng.li
  • 8,237
  • 2
  • 23
  • 30