It is impossible to understand the notation: * and ** with pointers

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

*name 

I understand that this is a pointer.

I searched, but I do not understand what this does, and did not find useful information.

 **name 

Context is an int ** name, not a multiplication

Can someone help me?

+6
source share
5 answers

NOTE. Without the right context, using *name and **name ambiguous. it can be a portrait (a). dereference operator (b) multiplication operator

Given what you are talking about a script like

  • char * name;
  • char **name;

in code

  • *name

name is a pointer to char .

  • **name

name is a pointer to a pointer to a char .

Please do not confuse with the "double pointer", which is sometimes used to refer to a pointer to a pointer, but should in fact mean a pointer to a variable of type double type.

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 a pointer. Hopefully this will become clear now.

+18
source

Actually it is very simple, think:

 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 each level as a different type of variable (so see * int as a type), this is easier to understand. Another example:

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

Hope this helps!

+8
source

the pointer stores the address of the variable, the pointer to the pointer stores the 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

+5
source
 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; 
0
source

** name in this case. It will be a pointer to a pointer.

-2
source

Source: https://habr.com/ru/post/985326/


All Articles