gpt4 book ai didi

c++ - 定义一个指针来引用相同的变量名引用?

转载 作者:太空狗 更新时间:2023-10-29 20:12:47 24 4
gpt4 key购买 nike

在 objective-c 中,我真的无法理解这一点:

void *x = &x;

我的理解是:

声明一个通用指针(因此类型为 void*),指针变量名称为 x,并且此指针指向对变量的引用(应该已经声明 - 但事实并非如此)名为 x。

让我很困惑!

最佳答案

断定是否该声明

void *x = &x;

是有效的,您应该考虑 C 标准中的两个重要引用。

第一个说标识符的范围从哪里开始(6.2.1 标识符的范围)

7 Structure, union, and enumeration tags have scope that begins just after the appearance of the tag in a type specifier that declares the tag. Each enumeration constant has scope that begins just after the appearance of its defining enumerator in an enumerator list. Any other identifier has scope that begins just after the completion of its declarator.

第二个说任意类型的指针是否可以赋值给指向void的指针(6.3.2.3指针)

1 A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.

所以在这个声明中

void *x = &x;

变量 x 的范围在赋值运算符之前立即开始。它的完整类型是void *,可以赋给任何其他类型的指针。右边是 void ** 类型的表达式。根据第二个引用,它可以分配给 x,因为 x 是指向 void 的指针。

结果 x 将存储其自身的地址。

关于c++ - 定义一个指针来引用相同的变量名引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25683034/

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