42

What does it mean when an object has two asterisks at the beginning?

**variable
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
numerical25
  • 10,524
  • 36
  • 130
  • 209
  • 6
    @Andreas People jump all over the trivial questions, because they get lots of views (106 so far) and thus lots of upvotes. Hence [16 upvotes in 17 minutes](http://stackoverflow.com/questions/2893129/what-does-mean-in-c/2893145#2893145) for a relatively basic answer – Michael Mrozek May 23 '10 at 19:54
  • 1
    possible duplicate of [Uses for multiple levels of pointer dereferences?](http://stackoverflow.com/questions/758673/uses-for-multiple-levels-of-pointer-dereferences) – Paul R May 23 '10 at 21:27
  • 2
    @Andreas: I'm surprised there were so many direct answers to such an... how should I say, _indirect_ question. ;-) – James McNellis May 23 '10 at 22:01
  • 4
    In addition to being easy rep, simple questions aso have the widest audience. I could answer this question, you could answer this question, a person who's three weeks into his first CS class could probably answer this question, etc. So even if a lower percentage of people choose to answer, it's still likely to get a lot of answers. That's an unfortunate flaw in the SO system — rare knowledge is likely to receive less reward. – Chuck May 23 '10 at 22:26
  • 3
    Without any code surprised no-one offered up multiplication using a pointer. result = x **pointer; – Stephen Nutt May 24 '10 at 01:17
  • Possible duplicate of [What is \*\* in C++?](http://stackoverflow.com/questions/644981/what-is-in-c) – phuclv May 03 '17 at 02:56

12 Answers12

56

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
  • 390,455
  • 97
  • 512
  • 589
  • And what if you were to set z to 2 using just one point *z = 2 ?? – numerical25 May 23 '10 at 20:14
  • 4
    @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 May 23 '10 at 20:31
35

It is pointer to pointer.

For more details you can check: Pointer to pointer

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
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Incognito
  • 16,567
  • 9
  • 52
  • 74
6

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

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
6

Pointer to a pointer when declaring the variable.

Double pointer de-reference when used outside the declaration.

Mavrik
  • 2,580
  • 4
  • 19
  • 30
4

Pointer to a pointer.

mfabish
  • 190
  • 1
4

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
  • 41
  • 1
2

**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
  • 18,794
  • 5
  • 57
  • 67
2

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
  • 15,415
  • 15
  • 80
  • 144
1

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

1

Pointer to another pointer

HZhang
  • 223
  • 1
  • 3
  • 2
    If he's asking a question this basic, he probably needs the concept of a pointer explained, too. – agf Apr 14 '12 at 02:53
1

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

Jake Petroules
  • 23,472
  • 35
  • 144
  • 225
1

It's a pointer to pointer.

As in if *x means that it will contain an address of some variable then if i say

m=&x then m is shown as

int **m

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kraken
  • 23,393
  • 37
  • 102
  • 162