gpt4 book ai didi

c - C 中的指针 : why is the * needed in *address_of_x = &x when getting the address of x?

转载 作者:太空狗 更新时间:2023-10-29 15:54:09 24 4
gpt4 key购买 nike

假设我们有以下代码,它接受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/

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