假设我们有以下代码,它接受x
,在其中存储值4
,然后将x
的地址存储在一个指针变量:
int x = 4;
int *address_of_x = &x;
int *address_of_x
中的*
有什么用?
我了解 *
用于取消引用指针 - 它需要一个地址并告诉您存储在那里的内容。但是如果一个地址被存储了,那么我们要解引用什么呢?或者我们根本没有取消引用任何东西?为什么不写成:
int address_of_x = &x;
最佳答案
int *address_of_x = &x;
中的
*
未取消引用 @Eugene Sh .
它是类型声明的一部分:pointer to int .
int x = 4;
int *address_of_x = &x;
和
一样int x = 4; // declare x as int
int *address_of_x; // declare address_of_x as pointer to int
address_of_x = &x; // Assign the address of x to address_of_x
Why isn't it written as:
int address_of_x = &x;
&x
不是 int
。 &x
是一个int *
,一个指针(或地址)。指针不是整数。
关于c - C 中的指针 : why is the * needed in *address_of_x = &x when getting the address of x?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47437772/