473,387 Members | 1,890 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,387 software developers and data experts.

vague lvalue vs rvalue question

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

"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 rvalue of type `pointer to
T' and produces an lvalue of type `T'. The <value, typepairs
from unary `&' must be distinct for distinct lvalues[*], and there
may be only one value produced for any particular lvalue. This
makes it an invertible function; `*' is then defined as the inverse
function. While `*' is a well-behaved function on conventional
architectures, all that the language requires is that it be the
inverse of `&'. "

I don't see how one is the inverse of the other. Can someone provide a
concrete example?
Chad
Jun 27 '08 #1
2 1742
On Tue, 15 Apr 2008 18:23:15 -0700 (PDT), Chad <cd*****@gmail.com>
wrote in comp.lang.c:
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....eed3fda1d0ee46

"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 rvalue of type `pointer to
T' and produces an lvalue of type `T'. The <value, typepairs
from unary `&' must be distinct for distinct lvalues[*], and there
may be only one value produced for any particular lvalue. This
makes it an invertible function; `*' is then defined as the inverse
function. While `*' is a well-behaved function on conventional
architectures, all that the language requires is that it be the
inverse of `&'. "

I don't see how one is the inverse of the other. Can someone provide a
concrete example?
Given an lvalue L of type T, the & operator produces something called
"the address of L", which has the type "pointer to type T".

Given a pointer to type T, the * operator access the lvalue of type T
that the pointer points to (assuming, of course, that it actually is
initialized to point to a type T object).

How about a concrete example:

#include <stdio.h>

int main(void)
{
int i = 3;
int *ip = &i;
double d = 1.5;
double *dp = &d;

printf("i = %d and *ip = %d\n", i, *ip);
printf("d = %f and *dp = %f\n", d, *dp);

return 0;
}

Do you understand why the output is:

i = 3 and *ip = 3
d = 1.500000 and *dp = 1.500000

I hope so. Notice that the second value is each output line is taken
by applying the * operator to a value that was obtained by the &
operator.

But we can make it more explicit by replacing the two printf() calls
in the program by these:

printf("i = %d and *ip = %d\n", i, *&i);
printf("d = %f and *dp = %f\n", d, *&d);

This might get you a warning message from your compiler that you have
assigned values to "ip" and "dp" that are never used, but when you
build and run the program you will get exactly the same output.

You are taking the address of 'i' or 'd' with &, then immediately
using '*' to dereference that address and retrieve its contents. You
are just doing it directly, without storing the addresses in
intermediate pointer objects.

So applying & to an lvalue produces an address rvalue, and applying *
to that rvalue produces the lvalue.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jun 27 '08 #2
Chad wrote:
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....eed3fda1d0ee46

"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 rvalue of type `pointer to
T' and produces an lvalue of type `T'. The <value, typepairs
from unary `&' must be distinct for distinct lvalues[*], and there
may be only one value produced for any particular lvalue. This
makes it an invertible function; `*' is then defined as the inverse
function. While `*' is a well-behaved function on conventional
architectures, all that the language requires is that it be the
inverse of `&'. "

I don't see how one is the inverse of the other. Can someone provide a
concrete example?
int thing = 42;
int *ptr = &thing;
assert(*&thing == thing);
assert (&*ptr == ptr);

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jun 27 '08 #3

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...
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: 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. ...
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: Lighter | last post by:
How to read "The lvalue-to-rvalue, array-to-pointer, and function-to- pointer standard conversionsare not applied to the left expressions"? In 5.18 Comma operator of the C++ standard, there is a...
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?
1
by: subramanian100in | last post by:
Consider the following: int x; int y; int z; (x+y) = z; For this statement, I get the following error with g++ compiler: error: non-lvalue in assignment Suppose I have a class Test and x,...
9
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...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.