假设
很多时候,我会感到困惑。请问有人能够澄清这些陈述并给出一些具体的例子吗?
*ptr
指向一个变量。那么*ptr
、&ptr
和ptr
分别代表什么意思?很多时候,我会感到困惑。请问有人能够澄清这些陈述并给出一些具体的例子吗?
在一个函数中使用以下变量。
int i = 0;
int* ptr = &i;
在该函数中,内存布局可能如下所示:+---+---+---+---+
| 0 |
+---+---+---+---+
^
|
Address of i
ptr
指向的内存:
+---+---+---+---+
| address of i |
+---+---+---+---+
^
|
Address of ptr
在上述场景中,
*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.
希望这讲得通。
int i = 1023
printf(..., i);
// out: 1023
i
的值,只需要执行以下代码:printf(..., &i);
// out: 0x4
假设我想要记住变量 i
存储的位置:
int *i_ptr = &i; // i_ptr is a variable of type int *
printf(..., i_ptr);
// out: 0x04
但是如果只打印出 i
的值,我需要一个 *
:
printf(..., *i_ptr); // * also doubles as a way to follow the pointer
// out: 1023
或者我可以直接打印出 i_ptr
存储的位置:
printf(..., &i_ptr);
// out: 0x32
给定以下声明
int i = 0x01234567;
int *ptr = &i;
Expression Type Value
---------- ---- -----
ptr int * Address of i
*ptr int Value stored in i (0x01234567)
&ptr int ** Address of ptr variable
在内存中,它看起来会像下面这样(假设是32位的int
)
Item Address 0x01 0x02 0x03 0x04
---- ------- ---- ---- ---- ----
i 0x7fffbb00 0x01 0x23 0x45 0x67
ptr 0x7fffbb04 0x7f 0xff 0xbb 0x00
因此,ptr
的值为0x7fffbb00
,&ptr
的值为0x7fffbb04
,*ptr
的值为0x01234567
。
假设你有:
int number = 5;
int *ptr = &number;
ptr将是变量,类型为int *,它将存储一个地址,比如0xf0f0f004,而不是存储5或"foo"。
*ptr将是这个内存地址0xf0f0f004中的值,在这种情况下是5。
&ptr将是存储变量的地址,比如0xff00ffaa。
ptr和number存储在内存的不同位置,两者都存储值,其中一个仅存储数字,另一个存储地址。语言允许您使用*来访问此地址的值。
*ptr
指向一个变量,那么这意味着*ptr
本身就是一个指针。这意味着您声明ptr
如下:int **ptr;
在这种情况下:
ptr
是指向指向您的变量的指针*ptr
是指向您的变量的指针&ptr
给出了指向指向指向您的变量的指针的地址//set up work
int x;
int *intermediatePtr;
int **ptr;
x = 5;
intermediatePtr = &x; //used only for additional level of indirection in this example
ptr = &intermediatePtr; //ie *ptr = &x;
//should print out the memory address for the pointer to the address of x
printf("%d", ptr);
//should print out the memory address of x (different memory location than above)
printf("%d", *ptr);
//should print out the memory address of the variable
//which holds the memory address of x
//(again, different memory location than both above examples)
printf("%d", &ptr);
ptr
指向一个变量而不是 *ptr
(即 int *ptr; ptr = &x;
),则比我的上面的示例少一级间接性。int *ptr; // declares a pointer to integer.
int val; // declares an integer.
&val; // pointer to val
ptr = &val; // ptr will now point to val
*ptr = 5; // same as writing val = 5
ptr++; // pointer to the next int after val (in this case, it will be undefined behaviour, it is useful if this pointer pointed to a cell in an array)
int arr[10]; // declares an array of size 10
ptr = arr; // ptr now points to arr[0] (name of an array can be implicity cast to pointer to first element)
ptr++; // ptr now points to arr[1]
&
= 引用*
= 取消引用ptr
是标识符/名称。您可以通过 int *ptr
声明它。
这是一个小的 C 程序:
#include<stdio.h>
int main()
{
int *ptr,ref;
ptr= &ref;
ref=12;
printf("%d\n",*ptr );
}
int *ptr
是一个整数指针。您可以使用其他数据类型,如浮点数、字符等。ptr = &ref
获取 ref
的地址并存储在 ptr
中。由于我们将 ptr
声明为整数指针,因此当我们打印所指向的值时,必须使用 *
取消引用。
ptr
在这三个中是相同的,它是一个变量。其中两个有操作符对该变量进行操作。 - chris