简体   繁体   English

在 c 中使用带有 ** 的指针

[英]using pointers that have ** in c

I am very new to C and currently having some trouble with pointers and I am not sure if my logic is correct on this question clarification would be great.我对 C 很陌生,目前在使用指针时遇到了一些问题,我不确定我的逻辑是否正确,澄清这个问题会很棒。

Is the second expression legal?第二个表达合法吗? Why or Why not?为什么或者为什么不? What does it mean?这是什么意思?

int** x = ....;
... **x ...

This is all the question gives and I came up with the following answer (I think its in the ballpark)这是所有的问题,我想出了以下答案(我认为它在球场上)

The int** x will initialize a pointer x to whatever address/value that is after the equal sign. int** x 将初始化一个指针 x 指向等号后面的任何地址/值。 **x ... will dereference the pointer to a value/variable **x ... 将取消引用指向值/变量的指针

The question link that was proposed in the edit was just showing the difference between int* p and int *p which is nothing what i asked i understand this already.编辑中提出的问题链接只是显示了 int* p 和 int *p 之间的区别,这不是我问的问题,我已经理解了。

int *x is a pointer to int . int *x是指向int的指针。 int** y defines y as a pointer to pointer to int , so it points to a pointer, and the latter points to an int . int** y定义y作为指针以指向int ,所以它指向一个指针,后者指向一个int Example:例子:

int n = 42;
int* x = &n; // points to n
int** y = &x; // points to x, which points to n

In this case y is a pointer to pointer.在这种情况下y是指向指针的指针。 First indirection *y gives you a pointer (in this case x ), then the second indirection dereferences the pointer x , so **y equals 42 .第一个间接引用*y为您提供一个指针(在本例中为x ),然后第二个间接引用取消引用指针x ,因此**y等于42

Using typedef s makes everything looks simpler, and makes you realize that pointers are themselves variables as any other variables, the only exception being that they store addresses instead of numbers(values):使用typedef使一切看起来更简单,并使您意识到指针本身与任何其他变量一样都是变量,唯一的例外是它们存储地址而不是数字(值):

typedef int* pINT;

int n = 42;
pINT x = &n;
pINT* y = &x; // y is a pointer to a type pINT, i.e. pointer to pointer to int

If you use double * you are telling the compiler you want a pointer pointing to another pointer:如果你使用 double * 你告诉编译器你想要一个指向另一个指针的指针:

int x = 4;
int *pointer = &x;
int **pointer_pointer = &pointer;
printf("%d\n",**pointer_pointer); // This should print the 4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM