很难说出星号与指针的关系.
以下是一些令我困惑的代码示例.
typedef struct X { int i_; double d_; } X;
X x;
X* p = &x;
p->d_ = 3.14159; // dereference and access data member x.d_
(*p).d_ *= -1; // another equivalent notation for accessing x.d_
Run Code Online (Sandbox Code Playgroud)
在第五行,我们有(*p).d_ *= -1;.为什么有两个星号?他们的立场意味着什么?
int x = 2;
int* p_x = &x; // put the address of the x variable into the pointer p_x
*p_x = 4; // change the memory at the address in p_x to be 4
assert(x == 4); // check x is now 4
Run Code Online (Sandbox Code Playgroud)
在第2行int* p_x = &x;,我们创建一个新指针,但在第3行*p_x = 4;,指针语法仍然使用星号.为什么会这样?
信用:"取消引用"指针是什么意思?代码示例.
注意:问题是"在C指针语法中,为什么间歇性地使用星号?".我改变它以帮助未来的访客.