Celeritas
Celeritas

Reputation: 15091

Why would anyone ever need a pointer to a pointer?

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.

Upvotes: 3

Views: 449

Answers (7)

someone_ smiley
someone_ smiley

Reputation: 1066

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;
}

Upvotes: 11

xiaofeng.li
xiaofeng.li

Reputation: 8587

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

Upvotes: 0

old_timer
old_timer

Reputation: 71566

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.

Upvotes: 3

Jack
Jack

Reputation: 761

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.

Upvotes: 2

Csaba Toth
Csaba Toth

Reputation: 10729

  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.

Upvotes: 0

iammilind
iammilind

Reputation: 70048

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

Upvotes: 1

Lindydancer
Lindydancer

Reputation: 26134

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

Upvotes: 10

Related Questions