0

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?

Community
  • 1
  • 1
Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406
  • Related, maybe not quite a duplicate sort-ofishly: [Uses for multiple levels of pointer dereferences?](http://stackoverflow.com/questions/758673/uses-for-multiple-levels-of-pointer-dereferences) – James McNellis May 23 '10 at 21:11

7 Answers7

7

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
  • 7,895
  • 1
  • 22
  • 23
  • Then again, why would you ever want to allocate an array of anything with operator new? – avakar May 23 '10 at 21:09
  • 2
    @avakar: to allocate an array of char* for instance? – nico May 23 '10 at 21:11
  • 3
    @nico: On StackOverflow I have received the advice to use a vector of chars rather than use `new`. – dreamlax May 23 '10 at 21:12
  • 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 May 23 '10 at 21:14
  • 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 May 23 '10 at 21:18
  • @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 May 23 '10 at 21:51
  • 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 May 23 '10 at 21:59
5

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
  • 104,103
  • 58
  • 317
  • 552
  • +1 Must not be a pointer to a string though, could be any data. – Felix Kling May 23 '10 at 21:13
  • @Felix Kling: Yes, it could be any data; but strings are the only thing I've ever needed it for. – Billy ONeal May 23 '10 at 21:25
  • 1
    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 May 23 '10 at 21:54
3

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
  • 47,994
  • 7
  • 61
  • 70
  • It is often used when the function returns some error/success value, i.e. init functions. `Handler *handler; if (init(&handler)) {...}` with `int init(Handler **handler) { if (!check_stuff()) return 0; *handler = get_new_handler(); return 1; }` –  May 23 '10 at 21:17
  • @dbemerlin: Yes, exactly. But I left out the clutter since it's orthogonal to pointer usage :) – Stephen May 23 '10 at 23:04
2

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...

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
2

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
  • 198,648
  • 61
  • 360
  • 533
1

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.
  • 147,927
  • 63
  • 340
  • 553
0

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
  • 62,085
  • 98
  • 303
  • 499