Not able to understand the notations : * and ** with pointers

妖精的绣舞 提交于 2019-12-23 09:26:59

问题


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?


回答1:


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

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.




回答2:


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!




回答3:


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




回答4:


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;



回答5:


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



来源:https://stackoverflow.com/questions/29692986/not-able-to-understand-the-notations-and-with-pointers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!