Not able to understand the notations : * and ** with pointers
Asked Answered
V

5

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?

Vintager answered 17/4, 2015 at 7:32 Comment(6)
pointer to pointer : tutorialspoint.com/cprogramming/c_pointer_to_pointer.htmFoment
Wow that was fast! Thanks all :)Vintager
It completely depends on the context in which *name and **name are.Outride
Well, obviously in 5 * name it's multiplication. But that is a binary (2 arg) operator *, not the unary (1 arg) operator*.Violent
@Violent You're thinking operators, yet the answerers are all thinking types. So, context matters.Outride
@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.Violent
T
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.

Thermometer answered 17/4, 2015 at 7:35 Comment(4)
@Outride Thanks sir for the advice. Updated accordingly.Thermometer
what does "pointer to a type" mean? You can't point to types in C++.Piccolo
@Piccolo I tried generelizing. , data and function combined. Any better word suggestion, please?Thermometer
In your first case I'd say "name is a pointer to char" and in the second case "name is a pointer to a pointer to a char".Piccolo
K
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!

Koreykorff answered 17/4, 2015 at 7:36 Comment(0)
F
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

Foment answered 17/4, 2015 at 7:36 Comment(0)
P
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;
Procrastinate answered 17/4, 2015 at 8:6 Comment(0)
P
-2

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

Pettiford answered 17/4, 2015 at 7:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.