6

I have a problem with the pointers. I know what this does:

*name

I understand that this is a pointer.

I've been searching but I do neither understand what this one does nor I've found helpful information

**name

The context is int **name, not multiplication

Could someone help me?

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
MLMH
  • 141
  • 8
  • pointer to pointer : http://www.tutorialspoint.com/cprogramming/c_pointer_to_pointer.htm – utarid Apr 17 '15 at 07:34
  • Wow that was fast! Thanks all :) – MLMH Apr 17 '15 at 07:37
  • 4
    It completely depends on the context in which `*name` and `**name` are. – juanchopanza Apr 17 '15 at 07:37
  • Well, obviously in `5 * name` it's multiplication. But that is a **binary** (2 arg) `operator *`, not the **unary** (1 arg) `operator*`. – MSalters Apr 17 '15 at 07:39
  • 2
    @MSalters You're thinking operators, yet the answerers are all thinking types. So, context matters. – juanchopanza Apr 17 '15 at 07:43
  • @juanchopanza: For a beginner, the only context that matters is the use of a unary operator versus the use as a binary operator. Because type definition intentionally mirrors use, it doesn't matter here whether we discuss `int **name` or `std::cout << **name`. – MSalters Apr 17 '15 at 07:47

5 Answers5

19

NOTE: Without the proper context, the usage of *name and **name is ambiguous. it may portrait (a). dereference operator (b) multiplication operator

Considering you're talking about a scenario like

  • char * name;
  • char **name;

in the code,

  • *name

name is a pointer to a char.

  • **name

name is a pointer, to the pointer to a char.

Please don't get confused with "double-pointer", which is sometimes used to denote pointer to a pointer but actually supposed to mean a pointer to a double data type variable.

A visual below

enter image description here

As above, we can say

char value = `c`;
char *p2 = &value;   // &value is 8000, so p2 == 8000, &p2 == 5000
char **p1 = &p2;     // &p2 == 5000, p1 == 5000

So, p1 here, is a pointer-to-pointer. Hope this make things clear now.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
8

It's actually very simple, consider this:

int a; // a is an int
int* b; // b is a pointer to an int
int** c; // c is a pointer to a pointer to an int

If you see every level as just another variable type (so, see *int as a type), it's easier to understand. Another example:

typedef int* IntPointer;
IntPointer a; // a is an IntPointer
IntPointer* b; // b is a pointer to an IntPointer!

Hope that helps!

Bas in het Veld
  • 1,271
  • 10
  • 17
5

pointer stores address of variable, pointer to pointer stores address of another pointer.

int var
int *ptr;
int **ptr2;

ptr = &var;
ptr2 = &ptr;

cout << "var : " << var;
cout << "*ptr : " << *ptr;
cout << "**ptr2 : " << **ptr2;

You can look here

utarid
  • 1,642
  • 4
  • 24
  • 38
0
int a = 5;// a is int, a = 5.
int *p1 = &a; // p1 is pointer, p1 point to physical address of a;
int **p2 = &p1; // p2 is pointer of pointer, p2 point to physical adress of p1;

cout<< "a = "<<a << " *p1 = "<<*p1<<" *(*p2) = " << *(*p2)<<endl;
Robust
  • 2,415
  • 2
  • 22
  • 37
-2

**name in this case. Would be a pointer to a pointer.

Luigi
  • 439
  • 5
  • 23