Topic: lvalue-to-rvalue conversion and a array-to-pointer
Author: Tom Payne <thp@cs.ucr.edu>
Date: Wed, 10 Jan 2001 00:10:28 GMT Raw View
Ron Natalie <ron@spamcop.net> wrote:
> Tom Payne wrote:
[...]
>> The name of an array does not designate that array and is not an
>> lvalue.
> Say what? An array name most certainly is an LVALUE and does designates
> the array.
>> the array's name designates the address of the array's zero-th element.
>> That fact is called "array-to-pointer conversion."
> If the array name was the address, then it would not need a conversion.
> The array to pointer conversion converts the array name to a pointer.
Right. Instead of "array name", I should have written "an expression
consisting of just an array name."
Thanks for the correction.
Tom Payne
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Ron Natalie <ron@spamcop.net>
Date: Wed, 10 Jan 2001 20:01:05 GMT Raw View
Tom Payne wrote:
>
>
> > If the array name was the address, then it would not need a conversion.
> > The array to pointer conversion converts the array name to a pointer.
>
> Right. Instead of "array name", I should have written "an expression
> consisting of just an array name."
>
It's still wrong.
It's not converted to a pointer until you do something with it that
mandates such a conversion. Argue as you might that arrays aren't
really separate types they are, they're just brain damaged.
The other aggregate types were fixed around 1979 in C, but arrays
remained second class citizens.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: James.Kanze@dresdner-bank.com
Date: Fri, 12 Jan 2001 04:54:39 GMT Raw View
In article <3A5C70A5.27A7760F@spamcop.net>,
Ron Natalie <ron@spamcop.net> wrote:
> Tom Payne wrote:
> > > If the array name was the address, then it would not need a
> > > conversion. The array to pointer conversion converts the array
> > > name to a pointer.
> > Right. Instead of "array name", I should have written "an
> > expression consisting of just an array name."
> It's still wrong.
> It's not converted to a pointer until you do something with it that
> mandates such a conversion.
Right. And it isn't just array names where this occurs, it is any
expression which has an array type (e.g.: dereferencing a pointer).
There are no special rules for "names" anywhere in an expression.
> Argue as you might that arrays aren't really separate types they
> are, they're just brain damaged. The other aggregate types were
> fixed around 1979 in C, but arrays remained second class citizens.
I don't think I've heard it better expressed.
--
James Kanze mailto:kanze@gabi-soft.de
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
Ziegelh ttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627
Sent via Deja.com
http://www.deja.com/
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Tom Payne <thp@hill.cs.ucr.edu>
Date: Mon, 8 Jan 2001 17:40:29 GMT Raw View
axter@my-deja.com wrote:
: Could someone please explain to me what is the difference between a
: lvalue-to-rvalue conversion and a array-to-pointer conversion?
: I'm trying to desifer (5.2.2 section 7) in the ANSI C++ standard.
An object is a segment of memory that hold (representations for) data
values.
An lvalue is an expression that designates an object.
Whenever an lvalue that designates a T-valued object appears where a
value of type T would be appropriate, the lvalue's result is converted
from the designated T-valued object to that object's current value.
That conversion is called "lvalue-to-rvalue conversion."
The name of an array does not designate that array and is not an
lvalue. Rather, in all contexts except sizeof(...) and &... the
array's name designates the address of the array's zero-th element.
That fact is called "array-to-pointer conversion." (Keep in mind that
subscripting is not an operation on arrays but rather on pointers.)
The key distinction is that lvalue-to-rvalue conversion applies to
scalar lvalues, while array-to-pointer conversion applies to names of
arrays. The only thing that these conversions have in common is that
they apply to names of objects. (But lvalue-to-rvalue conversion is
not limited to names of objects.)
I'd appreciate corrections if I've got any of this wrong.
Tom Payne
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Ron Natalie <ron@spamcop.net>
Date: Mon, 8 Jan 2001 21:51:07 GMT Raw View
Tom Payne wrote:
>
> An object is a segment of memory that hold (representations for) data
> values.
The standard says storage not memory actually.
> The name of an array does not designate that array and is not an
> lvalue.
Say what? An array name most certainly is an LVALUE and does designates
the array.
> the array's name designates the address of the array's zero-th element.
> That fact is called "array-to-pointer conversion."
If the array name was the address, then it would not need a conversion.
The array to pointer conversion converts the array name to a pointer.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: James.Kanze@dresdner-bank.com
Date: Tue, 9 Jan 2001 18:32:22 GMT Raw View
In article <938upb$lv9$1@mortar.ucr.edu>,
Tom Payne <thp@hill.cs.ucr.edu> wrote:
> The name of an array does not designate that array and is not an
> lvalue. Rather, in all contexts except sizeof(...) and &... the
> array's name designates the address of the array's zero-th element.
> That fact is called "array-to-pointer conversion." (Keep in mind
> that subscripting is not an operation on arrays but rather on
> pointers.)
The name of an array does designate the array, at least before the
array to pointer conversion. Except for sizeof and &, however, there
aren't any operations defined on arrays, so the implicit conversion
takes place. It is also possible to bind an array to an appropriately
typed reference -- another case where the conversion doesn't take
place.
Note too that the conversion takes place on an array, *NOT* just the
name of an array. So it is also occurs with references to arrays and
the results of dereferencing a pointer to an array, which most often
occur as a result of this conversion being applied to an array of
arrays. If this last is confusing, consider:
int a[5][4] ; // Declares an array of 5 arrays of 4 int.
a[ 5 ] // a converts implicitly to pointer to array
// of 4 int, adding 5 doesn't change the
// type.
And just to add to the confusion: if the declaration of a function
parameter type is array of ..., it is converted to pointer of ... But
this only occurs at the first level, so that
void f( int a[5][4] ) ;
takes an argument of pointer to array of 4 int. (And yes, the 5 is
completely ignored by the compiler, and can be considered simply
comment.)
Consider the following expressions, given the above declarations:
a[ 1 ][ 1 ] ; // This is the equivalent of:
*(*(a+1) + 1) // a converts implicitly to pointer to array
// of 4 int. Adding 1 returns a pointer to
// the following array of 4 int.
// Dereferencing gives us the actual array of
// 4 int. Which converts implicitly to a
// pointer to int, to which we add 1 and
// dereference to get the int.
f( &a[ 2 ] ) ; // This is the equivalent of:
f( &*(a + 2) ) ; // a converts implicitly to pointer to array
// of 4 int. Adding 2 returns a pointer to
// the second following array of 4 int. The
// &* effectively cancel out, and we have the
// correct type for the function.
And with references:
void g( int (a&)[4] ) ;
g( a[ 2 ] ) ; // Equivalent to:
g( *(a + 2) ) ; // As above, a converts to pointer to array
// of 4 int, +2 returns pointer to second
// following array of 4 int. Dereferencing
// gives the array itself. And since that
// is the required type, the implicit
// conversion does NOT occur.
In light of that sort of confusion, I'm glad we have std::vector.
> The key distinction is that lvalue-to-rvalue conversion applies to
> scalar lvalues, while array-to-pointer conversion applies to names
> of arrays.
No. All conversions apply to expressions, soly on the basis of type
and context. Whether the array is designated by name or by an
expression is irrelevant. The lvalue to rvalue conversion applies to
all types except arrays and functions. The array-to-pointer
conversion applies to arrays, and there is a similar conversion which
applies to functions.
> The only thing that these conversions have in common is that they
> apply to names of objects. (But lvalue-to-rvalue conversion is not
> limited to names of objects.)
No conversion is limited to names of objects. What they also have in
common is that the results are rvalues. But that is true of any
conversion which doesn't result in a reference type. So all they
really have in common is that they are conversions.
--
James Kanze mailto:kanze@gabi-soft.de
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
Ziegelh ttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627
Sent via Deja.com
http://www.deja.com/
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: James Kuyper <kuyper@wizard.net>
Date: Thu, 4 Jan 2001 18:59:14 CST Raw View
axter@my-deja.com wrote:
>
> Could someone please explain to me what is the difference between a
> lvalue-to-rvalue conversion and a array-to-pointer conversion?
> I'm trying to desifer (5.2.2 section 7) in the ANSI C++ standard.
The only thing that 5.2.2p7 says about those two conversions is that
they both occur during funcion calls. Therefore, your need to
distinguish them has nothing specifically to do with 5.2.2p7. They're
two very different things; you shouldn't be confusing them. I'd
recommend reviewing sections 4.1 and 4.2 (which are referenced in
5.2.2p7).
Here's an example:
void f(int value, int array*){
array[0] = value;
}
int a=3;
int b[4] = {1,2,3,4};
f(a,b);
The lvalue-to-rvalue conversion occurs for the first argument: 'a' is an
lvalue - you can write "a = 2", which will cause a change to value of
'a'. However, before being passed to f(), 'a' is converted to the rvalue
'3'. That value is used to initialize 'value'. Inside f(), the
expression "value = 2" has no effect on 'a'. That's what the
lvalue-to-rvalue conversion is all about.
'b' is defined as an array. However, f() is defined as taking a pointer
argument, not an array argument. When 'b' is passed as an argument to
f(), it gets automatically converted into a pointer of type 'int *',
which points to the first element of 'b'. That's the array-to-pointer
conversion, and it's what allows you to pass 'b' to f().
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: axter@my-deja.com
Date: Sun, 31 Dec 2000 04:25:17 GMT Raw View
Could someone please explain to me what is the difference between a
lvalue-to-rvalue conversion and a array-to-pointer conversion?
I'm trying to desifer (5.2.2 section 7) in the ANSI C++ standard.
Sent via Deja.com
http://www.deja.com/
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]