tags:

views:

189

answers:

7

Possible Duplicate:
Uses for multiple levels of pointer dereferences?

I saw a question about ** (pointer to a pointer) in C here. I'd like to know whats the point of this? When should I use it?

+1  A: 

This is called double indirection, meaning you have a pointer to another pointer, which in turn points to some useful data (or in rare cases yet another pointer).

There's a good article at Wikipedia that covers pointers in general and has a good section on double indirection.

Eric J.
+7  A: 

Whenever You need a pointer to a pointer :).

For example, if You want to dynamically allocate an array of pointers, operator new or malloc will return a pointer pointing to the first pointer in the array.

Other use might be, to pass a pointer to pointer, to a function, so the function can modify the original pointer. In C++, You can pass by reference, but not in C.

Maciej Hehl
Then again, why would you ever want to allocate an array of anything with operator new?
avakar
@avakar: to allocate an array of char* for instance?
nico
@nico: On StackOverflow I have received the advice to use a vector of chars rather than use `new`.
dreamlax
You need an x amount of pointers but you don't know how many you will need :P Then a pointer to a pointer is handy.
Ólafur Waage
To check the newly acquired knowledge, when reading a textbook, and before getting smart enough, to know better, and use a vector. Ok maybe new is a bad idea, but there are still environments in the wild, where malloc is all You have
Maciej Hehl
@dreamlax: well, although in 95% of the cases I'd use a `std::vector` for simple tasks I find using arrays easier. In any case if we're talking C there's no other option :)
nico
Note that this question is about __both C and C++__. In C, if you need a dynamically sized array of strings, a... um... _very popular way_ to do that is to have a pointer to the first element of an array of pointers to the first elements of character arrays. Thus doing so you will have a pointer to pointers, a `char**`.
sbi
Begging the question. Nice.
jeffamaphone
+5  A: 

When you have a function which wants to increment a pointer to a c-string. This is needed for things like recursive-descent parsers where each rule is responsible for incrementing things...

Billy ONeal
+1 Must not be a pointer to a string though, could be any data.
Felix Kling
@Felix Kling: Yes, it could be any data; but strings are the only thing I've ever needed it for.
Billy ONeal
Note that a pointer to a pointer is only necessary for this in C, in C++ you should pass a reference to a pointer for that. (And I really, really don't want to start any religious war over whether that makes more or less sense than the C way. I justed wanted to point out the fact.)
sbi
@sbi: Agreed. +1.
Billy ONeal
+2  A: 

It's commonly used for out parameters that are pointers, e.g.:

bool GetSomeObject(SomeObject** object) { 
    *object = new SomeObject();
    // ... initialize object or whatever ...
}

And you would call it like thus:

SomeObject* object;
if (GetSomeObject(&object)) {
    // ... use object ...
    delete object;
}

This is a common pattern where the callee allocates and the caller frees...

jeffamaphone
+3  A: 

In some variation of this:

void PutNewObjectHere(Class **dp) {
 *dp = new Class;
}

Class *p;
PutNewObjectHere(&p);
delete p;

(Note, this is a silly example for illustration. It would normally return the new pointer. The concept, however, does occasionally come up in practice)

Stephen
It is often used when the function returns some error/success value, i.e. init functions. `Handler *handler; if (init( *handler = get_new_handler(); return 1; }`
dbemerlin
@dbemerlin: Yes, exactly. But I left out the clutter since it's orthogonal to pointer usage :)
Stephen
A: 

In particular I've used these kind of pointers in the context of an array. For instance in my 8086 emulator project I had an array of function pointers. So it ended up looking like this:

instruction_functions[(int)opcode]();

This is also known as a jump table(sorta) and is used in a lot of places for optimization purposes.

Earlz
+2  A: 

You want a C function to mutate a pointer passed as argument, and it's C so there's no pass-by-reference. Therefore you pass a pointer to the pointer. Here's a favorite example adapted from Dave Hanson's C Interfaces and Implementations:

void Bit_free(struct Bit_T **set) {
    assert(set && *set);
    free(*set);
    *set = NULL;
}

By writing NULL into the freed pointer, you prevent it from dangling.

Norman Ramsey