473,395 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Lvalue?

Hi friends,

I read about Lvalue in previous posting and Googled it but I'm not
understood it completely.
There is a small doubt.

int a;
a=20; // here a is Lvalue

But

What is the Lvalue in the following example.

struct Node
{
int a;
int b;
};

struct Node tempNode1, tempNode2;

tempNode1.a=2;
tempNode1.b=3;

tempNode2=tempNode1; // What is the Lvalue in this case.
Thanks...

Nov 8 '06 #1
10 2897
the_init:
int a;
a=20; // here a is Lvalue

Correct.

But

What is the Lvalue in the following example.

struct Node
{
int a;
int b;
};

struct Node tempNode1, tempNode2;

tempNode1.a=2;
tempNode1.b=3;

tempNode2=tempNode1;

All of them are L-values. The rule of thumb is: "If it can appear on the
left-hand side of an assignment statement, then it is an L-value".

The L-values we're most familiar with are simple objects:

int a; a = 5;
double k; k = 4;

, but we can also obtain L-values from other places, e.g.:

*p = 5;

--

Frederick Gotham
Nov 8 '06 #2
"the_init" <th******@gmail.comwrites:
I read about Lvalue in previous posting and Googled it but I'm not
understood it completely.
There is a small doubt.

int a;
a=20; // here a is Lvalue

But

What is the Lvalue in the following example.
What do you mean by *the* lvalue? There isn't necessarily a single
lvalue in an expression or statement; there could be multiple lvalues,
or there could be multiple lvalues.
struct Node
{
int a;
int b;
};

struct Node tempNode1, tempNode2;

tempNode1.a=2;
tempNode1.b=3;

tempNode2=tempNode1; // What is the Lvalue in this case.
An lvalue is an expression that (potentially) designates an object.
Some expression contexts require an lvalue; others do not. The left
hand side of an assignment expression requires an lvalue (that's where
the name "lvalue" comes from; the 'l' stands for "left"). In the
assignment

tempNode2 = tempNode1;

both tempNode1 and tempNode2 happen to be lvalues, since they both
designate objects, but only tempNode2 is used in a context that
requires an lvalue.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 8 '06 #3
Frederick Gotham wrote:
...
All of them are L-values. The rule of thumb is: "If it can appear on the
left-hand side of an assignment statement, then it is an L-value".
...
No, actually. The rule of the thumb is: "If you can apply the unary '&' to it,
then it is an Lvalue". This also isn't absolutely accurate (especially if
treated as "then and only then" rule), but much closer to the truth that the
"assignment-based" version.

--
Best regards,
Andrey Tarasevich
Nov 8 '06 #4
Andrey Tarasevich said:
Frederick Gotham wrote:
>...
All of them are L-values. The rule of thumb is: "If it can appear on the
left-hand side of an assignment statement, then it is an L-value".
...

No, actually. The rule of the thumb is: "If you can apply the unary '&' to
it, then it is an Lvalue". This also isn't absolutely accurate (especially
if treated as "then and only then" rule), but much closer to the truth
that the "assignment-based" version.
The "assignment-based" version is even more broken than you suggest, at
least as currently stated. In the statement:

foo[6] = 42;

6 appears on the left side of an assignment statement, and yet 6 is not an
lvalue.

--
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.
Nov 8 '06 #5
Andrey Tarasevich wrote:
The rule of the thumb is: "If you can apply the unary '&' to it,
then it is an Lvalue".
This also isn't absolutely accurate (especially if
treated as "then and only then" rule),
but much closer to the truth that the
"assignment-based" version.
The two simplest exceptions are that
identifiers of functions can be operands of &,
but are not lvalues,
and identifiers of register class objects can't be operands of &,
but are lvalues.

--
pete
Nov 8 '06 #6
Richard Heathfield <in*****@invalid.invalidwrites:
Andrey Tarasevich said:
>Frederick Gotham wrote:
>>...
All of them are L-values. The rule of thumb is: "If it can appear on the
left-hand side of an assignment statement, then it is an L-value".
...

No, actually. The rule of the thumb is: "If you can apply the unary '&' to
it, then it is an Lvalue". This also isn't absolutely accurate (especially
if treated as "then and only then" rule), but much closer to the truth
that the "assignment-based" version.

The "assignment-based" version is even more broken than you suggest, at
least as currently stated. In the statement:

foo[6] = 42;

6 appears on the left side of an assignment statement, and yet 6 is not an
lvalue.
Of course, but that flaw is easily corrected by changing

If it can appear on the left-hand side of an assignment statement ...

to

If it can be the left operand of an assignment operator ...

It was a failure of wording, not of understanding. (Also, assignment
is an expression, not a statement.)

The "assignment-based" version is not *horribly* wrong; in fact, it's
the basis for the term "lvalue". I tend to use the idea as the basis
of a mnemonic ("lvalue" ... 'l' as in left, as in left-hand-side of an
assignment ... oh, yes, that's what it means) while keeping in mind
that it's not nearly that simple.

But if you want an actual definition of the term, an lvalue is just an
expression that (potentially) designates an object. (The word
"potentially" is necessary to allow for the fact that *ptr is an
lvalue even if ptr==NULL, something that both the 1990 and 1999 C
standards have screwed up.)

<OT>C++'s definition of "lvalue" differs from C's definition, but is
no more complicated. A recent length discussion of C++ was both
off-topic and needlessly obfuscated.</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 9 '06 #7
Andrey Tarasevich <an**************@hotmail.comwrites:
Frederick Gotham wrote:
>...
All of them are L-values. The rule of thumb is: "If it can appear on the
left-hand side of an assignment statement, then it is an L-value".
...

No, actually. The rule of the thumb is: "If you can apply the unary '&' to it,
then it is an Lvalue". This also isn't absolutely accurate (especially if
treated as "then and only then" rule), but much closer to the truth that the
"assignment-based" version.
I can't see why one rule of thumb is better than the other. Some
examples of what you had in mind would have helped. I tried to think
of how the two "rules" fail and came up with:

Expressions that are lvalues but

"can not be assigned to" "can not be the operand of unary &"
arrays bit-fields

Expressions that are not lvalues but

"can be assigned to" "can be the operand of unary &"
??? function names

What have I missed? Why is one rule "much closer" than the other?

--
Ben.
Nov 9 '06 #8
Ben Bacarisse <be********@bsb.me.ukwrites:
Andrey Tarasevich <an**************@hotmail.comwrites:
>Frederick Gotham wrote:
>>...
All of them are L-values. The rule of thumb is: "If it can appear on the
left-hand side of an assignment statement, then it is an L-value".
...

No, actually. The rule of the thumb is: "If you can apply the unary '&' to it,
then it is an Lvalue". This also isn't absolutely accurate (especially if
treated as "then and only then" rule), but much closer to the truth that the
"assignment-based" version.

I can't see why one rule of thumb is better than the other. Some
examples of what you had in mind would have helped. I tried to think
of how the two "rules" fail and came up with:

Expressions that are lvalues but

"can not be assigned to" "can not be the operand of unary &"
arrays bit-fields
I could have added string literals to this first columns since these
are lvalues that can not be assigned to, but modifying such an object
is "only" UB rather than a constraint volation!

--
Ben.
Nov 9 '06 #9
pete wrote:
...
>The rule of the thumb is: "If you can apply the unary '&' to it,
then it is an Lvalue".
>This also isn't absolutely accurate (especially if
treated as "then and only then" rule),
but much closer to the truth that the
"assignment-based" version.

The two simplest exceptions are that
identifiers of functions can be operands of &,
but are not lvalues,
Well, a better version of the rule would probably be the one that deals with
"objects" specifically.
and identifiers of register class objects can't be operands of &,
but are lvalues.
As are bit-fields. That's the reason I said it is not entirely accurate. Yet I
still believe that the '&'-based version better conveys the idea of lvalue as
something that has a place in storage.

--
Best regards,
Andrey Tarasevich
Nov 9 '06 #10
Ben Bacarisse wrote:
...
I can't see why one rule of thumb is better than the other. Some
examples of what you had in mind would have helped. I tried to think
of how the two "rules" fail and came up with:

Expressions that are lvalues but

"can not be assigned to" "can not be the operand of unary &"
arrays bit-fields

Expressions that are not lvalues but

"can be assigned to" "can be the operand of unary &"
??? function names

What have I missed? Why is one rule "much closer" than the other?
...
One reason I don't like the assignment-based version is because it discards a
large "class" of lvalues - constant objects. To me this does not feel like a
small and fairly negligible exception, acceptable in a "rule of thumb".

The assignment-based version correctly describes only one particular kind of
lvalues - _modifiable_ ones, - while completely ignoring the existence of
non-modifiable lvalues (of which arrays are another example).

--
Best regards,
Andrey Tarasevich
Nov 9 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Hongzheng Wang | last post by:
In K&R, they said: An object is a named region of storage; an lvalue is an expression refer to an object. How about these concept in C++? `The C++ Programming Language' has a similar...
9
by: Steven T. Hatton | last post by:
This is from the draft of the previous version of the Standard: http://www.kuzbass.ru:8086/docs/isocpp/expr.html 2- A literal is a primary expression. Its type depends on its form...
15
by: Michael Baehr | last post by:
I recently upgraded my Arch Linux system to GCC 3.4, and found out that a previously accepted behavior (cast-as-lvalue) is now marked as deprecated, and will cease to function in GCC 3.5. This has...
24
by: Romeo Colacitti | last post by:
Hi, Does anyone here have a strong understanding for the meanings of the terms "lvalue" and "rvalue" as it pertains to C, objects, and different contexts? If so please share. I've been...
9
by: junky_fellow | last post by:
Consider the following piece of code: (char *)0x100; /* I know that converting an integer to pointer type is implementation defined. But forget this for a moment */ My question is, Why the...
3
by: Kavya | last post by:
Can someone give and explain in simple terms a definition of lvalue? Also what are these modifiable and non-modifiable lvalues? I always thought that, if we can assign to anything then that...
14
by: nobrow | last post by:
Yes I know what lvalue means, but what I want to ask you guys about is what are all valid lvalues ... a *a a *(a + 1) .... What else?
6
by: Yarco | last post by:
I've alway thought lvalue means Left Value and rvalue means Right Value before i've read someone's article. It is said "lvalue = location value" and "rvalue = read value". Which one is right, then?
33
by: Pietro Cerutti | last post by:
Hi group, assume the following declarations: char *func_1(void); void func_2(char **); I am allowed to do: char *c = func_1();
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.