469,112 Members | 2,030 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,112 developers. It's quick & easy.

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 1523
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Hongzheng Wang | last post: by
9 posts views Thread by Steven T. Hatton | last post: by
14 posts views Thread by nobrow | last post: by
6 posts views Thread by Yarco | last post: by
1 post views Thread by subramanian100in | last post: by
reply views Thread by kglaser89 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.