3

For example:

bool insertInFront( IntElement **head, int data ){
    IntElement *newElem = new IntElement;
    if ( !newElem ) return false;

    newElen->data = data;
    *head = newElem; // Correctly updates head
    return true;
}

I am new to C++, coming from Java. I get the * for indirection syntax, but ** is not listed on this page: http://en.wikipedia.org/wiki/Operators_in_C_and_C++#Member_and_pointer_operators

I found this example on page 28 of Programming Interviews Exposed

Update

I realize that this question is naive, and I probably could have found an answer through other means. Obviously, I am new to the language. Still, asking "What does ** mean?" is not well supported online for someone who does not know that ** is a pointer operation. There are very few relevant results when searching C ** syntax or C++ ** meaning. Additionally, using ctrl + f to search ** in the wiki page above, and other documentation, doesn't return any matches at all.

I just wanted to clarify, from a beginner's perspective, that this question is hard to distinguish from the duplicates. Of course, the answer is the same :-) Thank you for the help.

modulitos
  • 14,737
  • 16
  • 67
  • 110

3 Answers3

10

There is no specific ** operator in C++, instead it's two separate asterisks, and asterisks in a declaration denotes pointer declaration.

So in the declaration

IntElement **head

the argument head is declared to be a pointer to a pointer to IntElement.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

Its meaning:

int a;                // integer
int *ptrA = &a        // pointer to a integer
int **PtrPtrA = &ptrA // point to pointer to a integer

How can it be used:

void function_nochange(int *pA  ) {   pA   = &b;  } 
void function_change  (int **ppA) {   *ppA = &b;  } 

int a;
int b;
void test()
{
  int *ptrA = &a

  function_nochange(ptrA)
  // here ptrA still point to int a since ptrA was copied

  function_change(&ptrA)
  // here ptrA point to int b since ptrA was passed as pointer 
} 
Phong
  • 6,600
  • 4
  • 32
  • 61
1

**VariableName means pointer to pointer(a chain of pointers) in C++

You can find good tutorials here :

http://www.tutorialspoint.com/cplusplus/cpp_pointer_to_pointer.htm

http://www.codeproject.com/Articles/4894/Pointer-to-Pointer-and-Reference-to-Pointer

snehal
  • 1,798
  • 4
  • 17
  • 24