C指针:*ptr vs&ptr vs ptr

vxs*_*122 4 c pointers

假设*ptr指向一个变量.这是什么*ptr,&ptr以及ptr各是什么意思?

很多时候,我对它们感到困惑.有人介意澄清这些陈述并给出一些具体的例子吗?

R S*_*ahu 9

在函数中使用以下变量.

int i = 0;
int* ptr = &i;
Run Code Online (Sandbox Code Playgroud)

在函数中,内存布局可能类似于:

内存对应于i:

+---+---+---+---+
|       0       |
+---+---+---+---+
^
|
Address of i
Run Code Online (Sandbox Code Playgroud)

内存对应于ptr:

+---+---+---+---+
| address of i  |
+---+---+---+---+
^
|
Address of ptr
Run Code Online (Sandbox Code Playgroud)

在上面的场景中,

*ptr == i == 0
ptr == address of i == address of memory location where the vale of i is stored
&ptr == address of ptr == address of memory location where the value of ptr is stored.
Run Code Online (Sandbox Code Playgroud)

希望有道理.