1

I've been practicing by looking at others code and I cannot understand the following concept:

int *ptr2int = &varInt;
int **ptr2ptr = &ptr2int;
int ***ptr2ptr2 = &ptr2ptr;

Why the first pointer declaration uses one *, the second uses two and the third uses three? How do pointers work exactly and what's * doing?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Jorge Gill
  • 175
  • 1
  • 2
  • 10
  • 1
    https://stackoverflow.com/questions/5580761/why-use-double-pointer-or-why-use-pointers-to-pointers – Antimon Oct 20 '18 at 12:21
  • 2
    The number of asterisks indicates a difference in type. `int*` can point to a non-pointer variable, `int**` can point to a pointer variable(int*) and `int***` can point to a pointer to a pointer. I wouldn't worry about `int***`, it's practically non-existent in any production code and even `int**` is extremely rare. – George Oct 20 '18 at 12:21
  • 1
    Not sure this will help or not. How I digest this concept is: I think of value as thing lets say ball and I put it in box which is memory address and when there is one * in pointer that means ball is wrapped in one box when two * are there then 2 boxes and so on. Each inner box is value for the outer box and outer box is address for inner one. This way compiler get to know how many deep a code is going to wrap or unwrap things. Not sure I am correct or not. – Aagam Jain Oct 20 '18 at 12:22

4 Answers4

3

The first pointer is a pointer to an int, so that's one *.

The second pointer is a pointer to a pointer to an int. So that 2 **.

Same for the third one.

A pointer represent the address of an object, in the first case the address of an int. Then you can have an address to an address pointing to an int.

etc.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
1

The *, in this context, means that the variable type is a "point of".
varInt is an int, so ptr2int that points to its address is a "pointer to int", or int *.
ptr2ptr points to the address of ptr2int, so it's a "pointer to a pointer to int", or int **.
ptr2ptr2 points to the address of ptr2int, so its a "pointer to a pointer to a pointer to int", or int ***.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

If there is exist at least one asterisk before the name of a variable then the variable is a pointer. The only things make pointer different regular variable is pointers are only used to store address( of an variable). Let look at below example:

int     *ptr2int = &varInt; //line 1
int*    *ptr2ptr = &ptr2int;//line 2
int**   *ptr2ptr2 = &ptr2ptr;//line 3

I have separated the asterisks for you to easily imagine.

  • At first line, because exist one asterisk before ptr2int so ptr2int will be a pointer, the ptr2int will be used to point to int variable(ptr2int will store address of an int value, in this case is address of varInt).
  • At second line, because exist one asterisk before ptr2ptr so ptr2ptr will be a pointer, but the pointer is not same with the pointer at the first line, the pointer at this line will be used to point to an int* variable( address of int* variable, in this case is address of ptr2int).
  • And so on for the third line, because exist one asterisk before ptr2ptr2 so it is a pointer, and the pointer will be used to store address of an int** variable. ptr2ptr is int** type.
TaQuangTu
  • 2,155
  • 2
  • 16
  • 30
0
int i = 0; // integer type

If I want to point to i I need a pointer to integer type:

int* ip; // pointer to integer type

If I want to point to ip I need a pointer to (pointer to integer) type:

int** ipp; // pointer to (pointer to integer) type

That's needed because ip is type int*. So a pointer to that is int**.

Whenever you need a pointer to a given type, you use a * in the declaration. Even if that type is already a pointer type.

So to point to a value, you need int*.

So to point to a pointer to a value, you need int**.

So to point to a pointer to a pointer to a value, you need int***.

So to point to a pointer to a pointer to a pointer to a value, you need int****.

etc...

Galik
  • 47,303
  • 4
  • 80
  • 117