Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Lvalue

Reply
Thread Tools

Lvalue

 
 
raghu
Guest
Posts: n/a
 
      11-14-2006
#include<stdio.h>
int main(void)
{
int a=1;
int b=2;
int c=0;
c=(a+b)++;
printf("%d",c);
return 0;
}

For the above program I got the error as Lvalue required.I compiled in
Turbo C/C++ compiler.

Can anyone please explain why the error is and how to eliminate it .

Thanks a lot.

Regards,
Raghu

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      11-14-2006
raghu wrote:
> #include<stdio.h>
> int main(void)
> {
> int a=1;
> int b=2;
> int c=0;
> c=(a+b)++;

White space isn't expensive and makes code easier to read.

(a+b) is an expression, not a modifiable value. Why not c = a+b+1?

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      11-14-2006
raghu said:

> #include<stdio.h>
> int main(void)
> {
> int a=1;
> int b=2;
> int c=0;
> c=(a+b)++;
> printf("%d",c);
> return 0;
> }
>
> For the above program I got the error as Lvalue required.


That's right, yes. ++ affects the value of an object. Arbitrary expressions
won't do.

> Can anyone please explain why the error is and how to eliminate it .


++ increases the value of an object by 1. (a+b) is not an object. To
eliminate the error, remove the ++:

#include<stdio.h>

int main(void)
{
int a = 1;
int b = 2;
int c = 0;

c = a + b;
printf("%d", c);

return 0;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      11-14-2006
Ian Collins said:

> raghu wrote:
>> #include<stdio.h>
>> int main(void)
>> {
>> int a=1;
>> int b=2;
>> int c=0;
>> c=(a+b)++;

> White space isn't expensive and makes code easier to read.
>
> (a+b) is an expression, not a modifiable value. Why not c = a+b+1?


Why the +1?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-14-2006
Richard Heathfield wrote:
> Ian Collins said:
>
>
>>raghu wrote:
>>
>>>#include<stdio.h>
>>>int main(void)
>>>{
>>>int a=1;
>>>int b=2;
>>>int c=0;
>>>c=(a+b)++;

>>
>>White space isn't expensive and makes code easier to read.
>>
>>(a+b) is an expression, not a modifiable value. Why not c = a+b+1?

>
> Why the +1?
>

I assumed he wanted to increment the sum of a and b.

--
Ian Collins.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      11-14-2006
Ian Collins said:

> Richard Heathfield wrote:
>> Ian Collins said:
>>
>>
>>>raghu wrote:
>>>
>>>>#include<stdio.h>
>>>>int main(void)
>>>>{
>>>>int a=1;
>>>>int b=2;
>>>>int c=0;
>>>>c=(a+b)++;
>>>
>>>White space isn't expensive and makes code easier to read.
>>>
>>>(a+b) is an expression, not a modifiable value. Why not c = a+b+1?

>>
>> Why the +1?
>>

> I assumed he wanted to increment the sum of a and b.


How would that affect the value of c?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-14-2006
Richard Heathfield wrote:
> Ian Collins said:
>
>
>>Richard Heathfield wrote:
>>
>>>Ian Collins said:
>>>
>>>
>>>
>>>>raghu wrote:
>>>>
>>>>
>>>>>#include<stdio.h>
>>>>>int main(void)
>>>>>{
>>>>>int a=1;
>>>>>int b=2;
>>>>>int c=0;
>>>>>c=(a+b)++;
>>>>
>>>>White space isn't expensive and makes code easier to read.
>>>>
>>>>(a+b) is an expression, not a modifiable value. Why not c = a+b+1?
>>>
>>>Why the +1?
>>>

>>
>>I assumed he wanted to increment the sum of a and b.

>
>
> How would that affect the value of c?
>

I assumed he wanted to increment the sum of a and b and assign it to c.

--
Ian Collins.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      11-14-2006
Ian Collins said:

> Richard Heathfield wrote:
>>

> I assumed he wanted to increment the sum of a and b and assign it to c.


If so, would he not have written ++(a + b) ?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-14-2006
Richard Heathfield wrote:
> Ian Collins said:
>
>
>>Richard Heathfield wrote:
>>
>>I assumed he wanted to increment the sum of a and b and assign it to c.

>
>
> If so, would he not have written ++(a + b) ?
>

Why?

--
Ian Collins.
 
Reply With Quote
 
mark_bluemel@pobox.com
Guest
Posts: n/a
 
      11-14-2006

Ian Collins wrote:
> Richard Heathfield wrote:
> > Ian Collins said:
> >
> >
> >>Richard Heathfield wrote:
> >>
> >>I assumed he wanted to increment the sum of a and b and assign it to c.

> >
> >
> > If so, would he not have written ++(a + b) ?
> >

> Why?


I think Richard's point was that the postfix "++" implies adding one
_after_ c had been assigned the value of "a+b"... Quite what the one
would be added to remains a mystery.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using a method as an lvalue and "invalid lvalue in assignment"compilation error markryde@gmail.com C Programming 11 09-22-2008 10:42 AM
operators requiring lvalue/rvalue operands and resulting in rvalue/lvalue Kavya C Programming 9 10-28-2006 01:45 AM
Perl hangs when returning lvalue closure from another lvalue closure Julian Mehnle Perl Misc 0 07-17-2003 03:13 PM
Re: lvalue with increment operators Victor Bazarov C++ 0 07-08-2003 01:44 AM
lvalue with increment operators srinivas reddy C++ 1 07-07-2003 05:27 PM



Advertisments