Jorge Gill
Jorge Gill

Reputation: 315

Why do I have to declare pointers this way?

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?

Upvotes: 1

Views: 224

Answers (4)

Galik
Galik

Reputation: 48615

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

Upvotes: 0

TaQuangTu
TaQuangTu

Reputation: 2343

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.

Upvotes: 0

Mureinik
Mureinik

Reputation: 311338

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

Upvotes: 1

Matthieu Brucher
Matthieu Brucher

Reputation: 22023

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.

Upvotes: 3

Related Questions