473,811 Members | 3,402 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=tempN ode1; // What is the Lvalue in this case.
Thanks...

Nov 8 '06 #1
10 2939
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=tempN ode1;

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=tempN ode1; // 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_Keit h) 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*****@invali d.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
"potentiall y" 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_Keit h) 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.comw rites:
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.comw rites:
>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

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

Similar topics

19
36666
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 explanation, however, it may be too brief. Can anyone give me a more detailed explanation? Or give some referrences
9
3559
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 (lex.literal). A string literal is an *lvalue*; all other literals are *rvalues*. -4- The operator :: followed by an identifier, a qualified-id, or an operator-function-id is a primary-expression. Its type is specified by the
15
4902
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 caused several builds to break, most notably elfutils. Presumably this behavior is not part of the C standard and it is thus being excised like many other nonstandard GCC extensions. Regardless, my question is not about the specifics or...
24
2969
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 reading several old posts/threads on the subject, and they never end with a conclusion (people keep correcting each other and disagreeing).
9
13799
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 above expression is not an lvalue ? It both specifies the the storage location as well as the type of object ? When I try to increment it
3
5771
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 anything is lvalue and if cannot assign to anything then that anything is not lvalue.
14
4387
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
3030
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
3003
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
9726
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10647
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10384
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10395
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6887
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.