-2

I'm newbie to C++ and i'm working through exercises on chapter 2.
I have a question on exercise 2.21.
This solution is given by Moothy found on GitHub

Exercise 2.21
Explain each of the following definitions.
Indicate whether any are illegal and, if so, why.
int i = 0;
(a) double* dp = &i;
Answer:
(a): illegal, cannot initialize a variable of type 'double *' with an rvalue of type 'int *'

I don't understand the last part of the answer " .. rvalue of int *".
The &i is the address of variable i and never was a pointer mentioned here.
Why would he mention a pointer here? And why an rvalue of a pointer to an int?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
yapkm01
  • 3,590
  • 7
  • 37
  • 62
  • 2
    `i` is an `int`, `&i` is the address of `i`, so a pointer to an `int`... – Luchian Grigore Jun 10 '15 at 15:52
  • &i is the address of variable i, which is of type (int *) and is an rvalue, because it is on the right hand side of the assignment... and that isn't automatically convertible to a pointer to a double, because it results in gibberish – Grady Player Jun 10 '15 at 15:53
  • @LuchianGrigore &i is an address of i. Yes. But there's no pointer (which in essence an object too) mentioned which store that address. It's confusing to say an address of an object is a pointer to that address. They looks like 2 different things to me – yapkm01 Jun 10 '15 at 15:57
  • @GradyPlayer Pls see my response to Luchian – yapkm01 Jun 10 '15 at 15:57
  • @GradyPlayer I just don't see &i == int * .. To me they are 2 distinct separate entity – yapkm01 Jun 10 '15 at 15:58
  • "It's confusing to say an address of an object is a pointer to that address." it is, but no one is saying that. An address of an object is (the value of) a pointer to that object. – Luchian Grigore Jun 10 '15 at 16:04
  • pointers aren't exactly an easy concept, and the notation that they chose to represent them maybe makes things less intuitive... but regardless ... `address of some var` , has a type, and if you want to store it the type that you use is `pointer to type of some var` – Grady Player Jun 10 '15 at 16:36
  • The last part of the answer is not "rvalue of `int *`" but "an rvalue *of type* `int*`". This should be read as `&i` is of type `int*` and `&i` is also an rvalue. You can't have `&i` on the left hand side of an expression. – Leiaz Jun 10 '15 at 16:48

2 Answers2

1

int* means a variable which can contain address of a integer variable. Now address of an integer variable when placed on the right hand side of the assignment then the left hand side should be int* otherwise it will give wrong results. That's what is told here.


Where is the pointer? dp is a pointer but not an integer pointer that's why the error.

rvalue- the value that appears in the rhs of assignment operator. A=B (r-value)

Do you know why we need different pointers? Suppose you have a pointer variable pointing to a chunk of integers.

BBBBBBB (bytes) 
| 
pointer(p) 

BBBBBBB (bytes) 
   |
   p+1 if pointing to int
BBBBBBB
 |
 p+1 if ointing to char 

Now if we say p++, then where do we move. Okay if it is pointing to int move( sizeof(int)) 4 bytes , if character 1 byte. Now say you are allowed to do wrong r-value assignment. then it will be very problematic. You have to careful enoough to code it properly.

Now hope you understand the whole picture.

Note:Look it is saying you can not initialize a double * with an r-value of int*. What is the r-value of an int *? it is address of an integer variable. So you are assigning to a double pointer an r-value of int* not r-value of double* which is an address of a double.

FROM MSDN

Every C++ expression is either an lvalue or an rvalue. An lvalue refers to an object that persists beyond a single expression. You can think of an lvalue as an object that has a name. All variables, including nonmodifiable (const) variables, are lvalues. An rvalue is a temporary value that does not persist beyond the expression that uses it. To better understand the difference between lvalues and rvalues, consider the following example:

example

For further example/clarification check this SO question

exact-difference-between-rvalue-and-lvalue

Community
  • 1
  • 1
user2736738
  • 30,591
  • 5
  • 42
  • 56
  • @yapkm01.:Check the answer..hope this will be helpful – user2736738 Jun 10 '15 at 16:11
  • Yes. i understand what an rvalue is. It's basically an expression where no location is specified .. e.g a temporary storage like in registry. I also do understand it's on RHS. But why would you say int * a rvalue eventhough it's on RHS? To me a int * an rvalue means there's no memory location for this int pointer. How about int* p = &i; Is int*p an rvalue? I know it's on the LHS but i just want to see what's the different int *p on LHS and int *p on RHS .. – yapkm01 Jun 10 '15 at 16:15
  • @yapkm01.:Look it is saying you can not initialize a `double *` with an `r-value of int*`. What is the r-value of an `int *`? it is address of an integer variable. So you are assigning to a `double pointer an r-value of int*` not r-value of `double*` which is an address of a `double`. – user2736738 Jun 10 '15 at 16:17
  • @yapkm01.: The explanation part is given in answer..check it. I – user2736738 Jun 10 '15 at 16:20
  • what's the difference int * p on the LHS and int *q on the RHS? why would you call int *p an ivalue and int *q a rvalue? Both has value which can be changed .. sorry .. see don't see the picture .. maybe later i will – yapkm01 Jun 10 '15 at 16:20
  • @yapkm01.: Look they seem same but on RHS it means that it must be assigned to proper l-value(that is one who can hold it(int*)). – user2736738 Jun 10 '15 at 16:23
  • @yapkm01.: Please upvote,downvote or accept the answer as that is what makes this post useful to future users. – user2736738 Jun 10 '15 at 16:33
  • i can't yet. I still don't see it. My confusion lies with why int * on RHS is consider rvalue. Int * is always assignable. It's not like comparing a=b+c and b+c=a .. yes this is clear cut since b+c is not assignable .. but an int * can always has its value changed – yapkm01 Jun 10 '15 at 16:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80205/discussion-between-coderredoc-and-yapkm01). – user2736738 Jun 10 '15 at 16:40
0

'dp' is a pointer to an double-value and the code wants it to point to an integer-value. This simply doesn't work. If 'dp' and 'i' weren't pointers, the value would be casted, but because you are working with pointers and not variables, this does not work.

Don't think to much about the given answer: That's compiler language and you will get a lot of sentences like that, but don't care about them now.