views:

816

answers:

13

What does it mean when a object has 2 asterisks at the beginning?

**variable
+4  A: 

It means that the variable is a pointer to a pointer.

SLaks
+3  A: 

Pointer to a pointer.

mfabish
+5  A: 

Pointer to a pointer when declaring the variable.

Double pointer de-reference when used outside the declaration.

Mavrik
+13  A: 

It is pointer to pointer. For more details you can check : Pointer to pointer

EDIT It can be good for example for dynamically allocating multidimensional arrays :

Like :

#include <stdlib.h>

int **array;
array = malloc(nrows * sizeof(int *));
if(array == NULL)
{
    fprintf(stderr, "out of memory\n");
    exit or return
}
for(i = 0; i < nrows; i++)
{
    array[i] = malloc(ncolumns * sizeof(int));
    if(array[i] == NULL)
    {
        fprintf(stderr, "out of memory\n");
        exit or return
    }
}
Incognito
It would also be a good idea to demonstrate properly freeing the multidimensional array in reverse order of allocation.
Adam Rosenfield
Yes you are right :). Not forget to use for(int i = 0; i < nrows; i++) free(array[i]); free(array); to free allocated memory.
Incognito
Thank you. That link was the most beneficial.
numerical25
+30  A: 

In a declaration, it means it's a pointer to a pointer:

int **x;  // declare x as a pointer to a pointer to an int

When using it, it deferences it twice:

int x = 1;
int *y = &x;  // declare y as a pointer to x
int **z = &y;  // declare z as a pointer to y
**z = 2;  // sets the thing pointed to (the thing pointed to by z) to 2
          // i.e., sets x to 2
Adam Rosenfield
And what if you were to set z to 2 using just one point *z = 2 ??
numerical25
@num It would change y to point to the memory location 2, and `*y = 1;` or `**z = 1;` would both try to change the memory at address 2, which is almost certainly outside the legal range you've been allocated
Michael Mrozek
+1  A: 

It is a pointer to a pointer. You can use this if you want to point to an array, or a const char * (string). Also, in Objective-C with Cocoa this is often used to point to an NSError*.

Time Machine
+1  A: 

**variable is double dereference. If variable is an address of an address, the resulting expression will be the lvalue at the address stored in *variable.

It can mean different things if it's a part of declaration:

type **variable would mean, on the other hand, a pointer to a pointer, that is, a variable that can hold address of another variable, which is also a pointer, but this time to a variable of type 'type'

Pavel Radzivilovsky
+1  A: 

It means that the variable is dereferenced twice. Assume you have a pointer to a pointer to char like this:

char** variable = ...;

If you want to access the value this pointer is pointing to, you have to dereference it twice:

**variable

Achim
+2  A: 

You can use cdecl to explain C-types.

There's an online interface here: http://cdecl.org/. Enter "int **x" into the text field and check the result.

Vimal
+1  A: 

Pointer to another pointer

HZhang
+1  A: 

** is a pointer to a pointer. These are sometimes used for arrays of strings.

Jake Petroules
+1  A: 

It is an example of a double pointer. i.e. pointer to pointer. Here address of pointer variable is stored in another pointer variable.

For more, see this tutorial.

Code Guru
A: 

its a pointer to pointer,. as in if *x means that it will contain an address of some variable than if i say m=&x than m is shown as int **m

hopefulLLl