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

C Primer练习2.25

发布时间:2020-12-16 10:34:58 所属栏目:百科 来源:网络整理
导读:在C Primer第5版的书中有一个我无法弄清楚的练习(2.25). Exercise 2.25: Determine the types and values of each of the following variables. (a) int* ip,r = ip; 现在,这本书就是这样的例子: int i = 42;int *p; // p is a pointer to intint *r = p; /
在C Primer第5版的书中有一个我无法弄清楚的练习(2.25).

Exercise 2.25: Determine the types and values of each of the following
variables.
(a) int* ip,&r = ip;

现在,这本书就是这样的例子:

int i = 42;
int *p; // p is a pointer to int
int *&r = p; // r is a reference to the pointer p

所以,我的问题是,为什么在那个练习中& r没有*运算符?写作有什么不同吗?

int *&r = ip;

要么

int &r = ip;

(其中ip是指向int的指针)

解决方法

我想这本书的作者认为签名int *将执行所有逗号分隔的变量声明,使r成为指针的引用.确实 the code does not compile因为这不是真的. ip被声明为指向int的指针,r仅被声明为对int的引用.

编译器解释

int * ip,&r = ip;

等价于

int * ip;
int & r = ip; // won't compile

你需要一个额外的*来声明它作为指针类型的引用:

int *op,*&r = ip;

您还可以使用typedef:

typedef int* IntPtr;
IntPtr op,&r = ip;

(编辑:李大同)

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

    推荐文章
      热点阅读