2

I am a beginner in programming and was trying out some combinations.

#include<stdio.h>


int main()
{
int a=5;

printf("%d",&a); // STATEMENT 1
printf("\n%d",a); //STATEMENT 2
printf("\n%d",&(a++)); //STATEMENT 3
printf("\n%d",a);  //STATEMENT 4

return 0;
}

I get a error in STATEMENT 3 saying

[Error] lvalue required as unary '&' operand

I expected the output of STATEMENT 1 & 3 to be same as both address are the same.

Also I expected the output of STATEMENT 2 to be 5 & STATEMENT 4 to be 6.

I looked up and found a similar question : Lvalue required error

I understood the issue in that question.From the comments to the first answer to above mentioned question I see lvalue as something to which something can be stored.

But I still can't understand why &(a++) or &(++a) should give an error. Any help will be appreciated.

Thank You for reading this.

[Edit] Thank you for answering. If possible please include references where the exact sequence of execution or nature of such expressions are discussed. That way rookies like me won't trouble the community with such trivial questions.

Community
  • 1
  • 1
kryptoknight
  • 264
  • 1
  • 3
  • 10
  • 1
    Question you linked is not actually related. That question concerns lvalue in `++` operator. In your case the problem is with `&` operator. You can replace `a++` with `a+1` and will get the same error. – Piotr Praszmo Jun 28 '14 at 21:32
  • 1
    And the linked question has an answer which is not fully correct. Array's _are_ lvalues. Ignore it, it won't help you here. – mafso Jun 28 '14 at 21:34

3 Answers3

6

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.

Jonas Bötel
  • 4,452
  • 1
  • 19
  • 28
  • 1
    note that `++a` is not an lvalue either. Also, the property of being an lvalue or not is a property of the expression itself, not a property of the "returned value". An expression *evaluates* to a value, and may have *side-effects*. In the case of `++` (either form), the increment is a side-effect, and the evaluation is to either `a` or `a+1`. In both cases it *could* be an lvalue if the language designers had wanted it that way , but they chose not to. (Note: in C++, the result of an increment is sometimes an lvalue). – M.M Jun 29 '14 at 14:44
  • 2
    A good reason for making these expressions non-lvalues is so that expressions like `a++ = 5;` give a compiler error (as opposed to being allowed, but silently cause undefined behaviour doe to updating `a` twice without an intervening sequence point). – M.M Jun 29 '14 at 14:45
  • @MattMcNabb +1 for providing a reason for why it might be so. – kryptoknight Jul 03 '14 at 02:10
2

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
Ayush
  • 2,608
  • 2
  • 22
  • 31
1

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).

Quentin
  • 62,093
  • 7
  • 131
  • 191
  • `++a` is not an lvalue either. Ref: 6.5.3.1#2 says that `++a` is equivalent to `a += 1`, and 6.5.16#3 says that the result of an assignment (simple or compound) is not an lvalue. – M.M Jun 29 '14 at 14:38
  • Prior to C11, `a++` and `++a` have identical properties, except that the "returned" value is `a+1` in the latter. – M.M Jun 29 '14 at 14:39
  • @MattMcNabb oh, so that's why what I found on the subject was rather inconsistent. Editing. – Quentin Jun 29 '14 at 15:33
  • I think you misread me; `++a` is not an lvalue in C++11 either. What changed in C++11 was the rules about sequencing – M.M Jun 29 '14 at 15:36
  • 1
    @MattMcNabb these rules don't change anything here then ? On a side note, I have trouble finding information on the reason why ++a is no lvalue. – Quentin Jun 29 '14 at 15:43
  • I gave references in my first comment to your answer – M.M Jun 29 '14 at 15:45
  • @MattMcNabb :The reference you have given belongs to which book? – kryptoknight Jul 03 '14 at 02:03