加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 定义指向同一变量名称引用的指针?

发布时间:2020-12-16 09:54:03 所属栏目:百科 来源:网络整理
导读:在 objective-c中,我真的无法理解这一点: void * x = x; 我的理解是: 声明一个泛型指针(因此类型为void *),指针变量名称为x,并且此指针指向对变量的引用(应该已经声明 – 但它不是已声明),名为x. 对我来说很困惑! 解决方法 要总结一下这个宣言 void *x =
在 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将存储自己的地址.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读