Not able to understand error condition wrt lvalues

一曲冷凌霜 提交于 2019-12-02 01:51:10

a++ increments your a and returns the old value of a. The & operator returns the address of a variable. But the returned old value of a doesn't have an address. It's a very temporary thing, not an lvalue. That's why you can not take the address of it.

a++ and ++a and & all three of them needs lvalue to operate on and when operated they return the rvalue

So when you do &(a++)

First, (a++) is performed  -> a is taken as lvalue and (a++) returns rvalue

Second, &(a++) is performed -> a++ returns rvalue but & needs lvalue so lvalue is
missing,therefore an error.

For this similar reason you can't perform any of these:

++(a++) : lvalue missing
(++a)++ : lvalue missing
a++++   : lvalue missing
++++a   : lvalue missing

a++, as you may know, returns a temporary with the value of a prior to incrementation. And you can't take the address of a temporary for the rather good reason that it'll die at the end of its expression (and thus before you manage to do anything with it).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!