C++基础问题:指针的引用
PHPz
PHPz 2017-04-17 13:16:13
0
2
531

C++ primer 第五版的练习题:

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

请问怎么理解 &r = ip ?

我实在理解不能。。。
r 是一个 int 的引用,如何可以用 ip (一个 int 的指针)赋值呢?

--
补充:

正如 @araraloren 在回答中指出的,
这句编译时会报错:

int* ip, &r = ip;

要改成:

int* ip, *&r = ip;

或者

int* ip, &r = *ip;

才可以编译通过。

PHPz
PHPz

学习是最好的投资!

reply all(2)
大家讲道理

The type of ip is int*
The type of r is int&
&r = ip; //The compiler will give an error
Maybe the idea of ​​the question is to distinguish the type, and the correctness of the code is second?

Ty80

&r is the address of r. No one asked to assign int* to r, but to &r, so *(&r)=*ip

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!