Why use pointers to pointers?

Pretty much what it sounds like. I know that using pointers is obviously quite useful in C++, but Ive never understood what the use of pointers to pointers could be. Wouldn't it just waste memory defining the adresses at the pointer to pointer level? Is there a hidden use for them, or was it just included for the sake of flexibility in C++?
Usually a pointer to a dynamically allocated array ends up being a pointer to a pointer, or a bunch of pointers in that case. MS's COM does this A LOT which is probably the most difficult part about it to wrap your head around. I've also caught myself doing it while I was playing around with Polymorphism too, you start out with an array of pointers to base objects, you find yourself wanting to pass it to a function and before you know it you end up dereferencing objects in order to dereference them.
You pass a pointer because you want to be able to change the value, correct? What if the value you need to change happens to be of pointer type?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

int x = 1;
int y = 2;

void change( int* int_ptr )
{
  int_ptr = &y;
}

void actuallyChange( int** int_ptr_ptr )
{
  *int_ptr_ptr = &y;
}

int main( void )
{
  int* int_ptr = &x;

  std::cout << "before change: " << *int_ptr << std::endl;
  change( int_ptr );
  std::cout << "after change : " << *int_ptr << std::endl;
  actuallyChange( &int_ptr );
  std::cout << "really change: " << *int_ptr << std::endl;

  return 0;
}


You would see this a lot more in C code than in C++.

Consider if you had to write a stack data structure in C, you might have something like this:
1
2
3
4
5
6
7
8
9
10
11
typedef struct node
{
  int value;
  node* next;
} Node;

int main( void )
{
  Node *my_stack;
  // ...
}


If you wanted to write a function that added to the stack, you need that function to do two things. The first is of course to create the new node and put it on the top of the stack. The second is that the my_stack pointer would have to now point to the new node. This means that you would not be able to pass my_stack by value, you would need to pass a pointer to my_stack ( which is a pointer )

void stack_push( Node** top, int new_value );
Last edited on
You'd use a pointer to a pointer for the same reasons you'd use a pointer to any other data type. For example, you might want to pass a pointer into into a function that changes the value of that pointer, and you might have a reason to prefer using a pointer to the pointer, rather than a reference to it.

Or you might want to have an array of pointers, just like you might want an array of ints. So you can manipulate the array using a pointer to a pointer, just like you'd manipulate an int array using a pointer to int.

A pointer is just a variable, like any other. You want the same ability to manipulate that variable type that you would any other type.
Thanks for the info.

But... Wouldnt editing the value of a pointer (ie the address in memory that the pointer is listed at) be rather dangerous?
No more so than assigning an initial value to it.

Which is to say - as long as you're assigning a valid memory address that you know contains valid data, it's fine. But that's true of the initial assignment, too.
But... Wouldnt editing the value of a pointer (ie the address in memory that the pointer is listed at) be rather dangerous?




Why would you think so?

1
2
3
4
5
6
7
8
9
int One = 1;
int Two = 2;
int *Ptr = &One; //Initializing the pointer

std::cout << *Ptr << std::endl;

Ptr = &Two; //Changing the address the pointer holds

std::cout << *Ptr << std::endl;


You could try to hard code the address of the pointer but, just like jumping out an window and trying to use your neck as a pogo-stick, it's really not a good idea.
yes...

Those arent dangerous, but I was thinking more along the lines of

1
2
float * pFloat = new float(5);
pFloat++;


which would define a dynamic float, then shift the address it points to by one byte in memory. That would cause strange behavior I think.
Last edited on
No, it wouldn't shift it by one byte, it would shift it by the size of one float (which would be more than 1 byte, but defined by the implementation). You might want to read up on pointer arithmetic.

It would be a silly thing to do in the case where your pointer points to a single float. On the other hand, it would be a perfectly normal thing to do if your pointer pointed to the first float in an array.

So... um... do sensible things and not stupid ones, I guess.
Oh right, yes, my mistake.
Topic archived. No new replies allowed.