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).
My take on it is that an "lvalue" is an expression that refers to an
object (which can have (a) value(s) within it), and "rvalue" is an
expression that only has a value (ephemeral value as Chris Torek would
claim) and no association with an object.
As far as their use, an "lvalue" that refers to an object of type T,
can be used anwhere an "rvalue" that that has a type T can be, but not
vice versa. So if one uses an lvalue that refers to an int variable in
an context that requires an int value, then simply the value sitting in
the object is dumped into that context.
Is this a fair description?
Nov 14 '05
24 2963
In article <42******@212.6 7.96.135>, "Bart C" <bc@freeuk.co m> wrote: "Romeo Colacitti" <ww*****@gmail. com> wrote in message news:11******** **************@ c13g2000cwb.goo glegroups.com.. . 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).
I always thought an lvalue was something you could take the address of using &.
So an assignment like:
a=b;
could be rewritten as:
*(&a)=b;
if a was a legal lvalue. If you can't then 'a' (whatever it might be) is not an lvalue.
Bitfields can be lvalues, but you can't take the address of a bitfield.
In article
<ch************ *************** ******@slb-newsm1.svr.pol. co.uk>,
Christian Bau <ch***********@ cbau.freeserve. co.uk> wrote: In article <42******@212.6 7.96.135>, "Bart C" <bc@freeuk.co m> wrote:
"Romeo Colacitti" <ww*****@gmail. com> wrote in message news:11******** **************@ c13g2000cwb.goo glegroups.com.. . 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).
I always thought an lvalue was something you could take the address of using &.
So an assignment like:
a=b;
could be rewritten as:
*(&a)=b;
if a was a legal lvalue. If you can't then 'a' (whatever it might be) is not an lvalue.
Bitfields can be lvalues, but you can't take the address of a bitfield.
And I forgot: Functions are most definitely not lvalues, but you can
take the address of a function.
Christian,
You wrote on Sun, 06 Feb 2005 22:53:25 +0000: > So an assignment like: > a=b; > could be rewritten as: > *(&a)=b; > if a was a legal lvalue. If you can't then 'a' (whatever it might be) is not an lvalue. Bitfields can be lvalues, but you can't take the address of a bitfield.
CB> And I forgot: Functions are most definitely not lvalues, but you can
CB> take the address of a function.
You also forgot that there are non-modifiable lvalues like those that are
const-qualified or that have incomplete types.
--
Ivan
Unicals Group -- Your own commercial high-quality C99 front end for US $1 http://unicals.com/own-business.html
In article <36************ *@individual.ne t>,
"Ivan A. Kosarev" <ik******@onlin e.ru> wrote: Christian, You wrote on Sun, 06 Feb 2005 22:53:25 +0000:
-- Re-inserted original post here: "Romeo Colacitti" <ww*****@gmail. com> wrote in message news:11******** **************@ c13g2000cwb.goo glegroups.com.. . 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).
I always thought an lvalue was something you could take the address of using &.
-- End of re-inserted original post > So an assignment like: > a=b; > could be rewritten as: > *(&a)=b; > if a was a legal lvalue. If you can't then 'a' (whatever it might be) is not an lvalue. Bitfields can be lvalues, but you can't take the address of a bitfield. CB> And I forgot: Functions are most definitely not lvalues, but you can CB> take the address of a function.
You also forgot that there are non-modifiable lvalues like those that are const-qualified or that have incomplete types.
Looks like I didn't forget anything; you just snipped the part of the
original post that I answered to.
Kobu wrote: Luke Wu wrote: When an lvalue is used in an "general object context," then the lvalue is directly acted upon (no conversion to an rvalue takes place). Examples are & and sizeof.
sizeof takes more than lvalues, consider...
sizeof(int *) sizeof('A') sizeof(33.029e-3LD)
so are types and constants objects too (note, size of directly taking the objects as input above, not just lvalues that refer to the
objects)
here is another example
sizeof("String Literal")
here, size is receiving only a pointer to the first element of the string ('S'), so its equivalent to: sizeof(char *)
but that's not what we get, sizeof actually returns the size of the whole string literal
I don't think sizeof fits cleanly with the theory of lvalues/rvalues.
Sizeof is an operator that can take two types of operands:
- types
- expressions(if lvalue expression, gives the size of the entire object
designated by the lvalue, if rvalue expression, gives the size of
object required to properly hold the rvalue)
When considering the sizeof(exp) syntax's behaviour, one might be
tempted to call this an example of an "object context." I argue that it
incorrect to say that sizeof(exp) is a case of "lvalue/object context."
Even though no lvalue-to-rvalue substitution takes plac for lvalue
expression, the fact that rvalues can be operands too should forbit us
from calling it an "object context." It is neither an "object context"
or a "value context", but rather a "makes no assumptions or
substitutions, operates directly on what you hand it CONTEXT"
This is why sizeof('A') works, not because 'A' is an lvalue or object
(constants are not objects).
Romeo Colacitti wrote: 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.
[...]
The simplest way to think of them would probably be:
An lvalue can go on the left of an assignment statement, and an
rvalue can go on the right. (Hence "l" and "r".)
--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>
"Christian Bau" <ch***********@ cbau.freeserve. co.uk> wrote in message
news:ch******** *************** **********@slb-newsm1.svr.pol. co.uk...
.... In article <42******@212.6 7.96.135>, "Bart C" <bc@freeuk.co m> wrote: > I always thought an lvalue was something you could take the address of > using > &.
.... Bitfields can be lvalues, but you can't take the address of a bitfield.
And I forgot: Functions are most definitely not lvalues, but you can take the address of a function.
I think I'm still right apart from these two exceptions..
Bart
Kenneth Bull wrote: Sizeof is an operator that can take two types of operands:
- types - expressions
The types must be object types
and the expressions must be expressions of object type.
--
pete
Christian Bau wrote: In article <ch************ *************** ******@slb-newsm1.svr.pol. co.uk>, Christian Bau <ch***********@ cbau.freeserve. co.uk> wrote:
In article <42******@212.6 7.96.135>, "Bart C" <bc@freeuk.co m> wrote:
"Romeo Colacitti" <ww*****@gmail. com> wrote in message news:11******** **************@ c13g2000cwb.goo glegroups.com.. . > 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).
I always thought an lvalue was something you could take the address of using &.
So an assignment like:
a=b;
could be rewritten as:
*(&a)=b;
if a was a legal lvalue. If you can't then 'a' (whatever it might be) is not an lvalue.
Bitfields can be lvalues, but you can't take the address of a bitfield.
And I forgot: Functions are most definitely not lvalues, but you can take the address of a function.
register qualified variables are lvalues without addresses.
--
pete
Kenneth Bull wrote: There are normal objects, and anonymous objects.
A search for the term - anonymous - in the C standard came up with 0
results.
Explanation for malloc does say that it allocates AN OBJECT.
Explanation for calloc does say that it allocates AN ARRAY OF OBJECTS.
No mention of anonymous though.
I've come to the conclusion that the C language (and it's standard) has
so many exceptions, loopholes and gray areas that it's better to learn
and see all the cases than to try to understand the definitions and
rules within the standard.
I bet every C programmer has a different idea for what all these terms
mean, but all experts have seen all the cases/uses enough to understand
things deep enough not to care for exact definitions. Such a
frustrating language, but I can't live without it :-) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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
|
by: Kavya |
last post by:
These were the questions asked to me by my friend
1. Operator which may require an lvalue operand, yet yield an rvalue.
2. Operator which may require an rvalue operand, yet yield an lvalue.
My answer to these questions are & and * respectively.
Is/Are there any other operator(s) satisfying these criteria?
|
by: Peter Lee |
last post by:
what's the correct behaver about the following code ? ( C++
standard )
I got a very strange result....
class MyClass
{
public:
MyClass(const char* p)
{
printf("ctor p=%s\n", p);
|
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?
| |
by: usao |
last post by:
Does anyone have an example of how to create a class which describes
and array, such that I can use the subscript operator on both the
left and right side of an assignment statement? This is as close as I
can get, but I feel it's kludgey and slow...
#include <iostream.h>
template<class T>
class foo {
T *data;
|
by: Chad |
last post by:
The following question actually stems from an old Chris Torek post.
And I quote from the following old CLC url
http://groups.google.com/group/comp.lang.c/browse_thread/thread/ce93f8bf3e61aede/cfeed3fda1d0ee46?hl=en&lnk=gst&q=convert+lvalue+to+rvalue#cfeed3fda1d0ee46
"Mathematically speaking, unary `&' and `*' are inverse functions.
Unary `&' takes an lvalue of type `T' and produces an rvalue of
type `pointer to T'; unary `*' takes an...
|
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...
|
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...
|
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,...
|
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...
| |
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |