gpt4 book ai didi

c - 如何判断星号的使用是否与指针相关?

转载 作者:太空宇宙 更新时间:2023-11-04 05:36:26 24 4
gpt4 key购买 nike

很难说出星号与指针的相关位置。

以下是一些让我感到困惑的代码示例。

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_

在第五行,我们有 (*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

在第 2 行 int* p_x = &x; 中,我们创建了一个新指针,但在第 3 行 *p_x = 4; 中,指针语法仍然使用星号。为什么会这样?

来源:What does "dereferencing" a pointer mean?代码示例。

注意:问题是“在 C 指针语法中,为什么间歇性地使用星号?”。我更改它以帮助 future 的访客。

最佳答案

(*p).memp->mem
相同*=中的*是乘法,它是乘赋值运算符,与指针无关

关于c - 如何判断星号的使用是否与指针相关?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34037510/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com