Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   conversion of array to pointer not limited to lvalues (http://www.velocityreviews.com/forums/t629127-conversion-of-array-to-pointer-not-limited-to-lvalues.html)

nicolas.sitbon@gmail.com 08-04-2008 05:44 PM

conversion of array to pointer not limited to lvalues
 
hi everybody, In the new feature of C99, i find " conversion of array
to pointer not limited to lvalues" : what does it mean? I don't find
any difference between C90 and C99 about conversion of array to
pointer.
Thanks.

Chris Torek 08-04-2008 06:34 PM

Re: conversion of array to pointer not limited to lvalues
 
In article <507c935b-8485-4a68-8038-a129f67b85ba@25g2000hsx.googlegroups.com>
<nicolas.sitbon@gmail.com> wrote:
>hi everybody, In the new feature of C99, i find "conversion of array
>to pointer not limited to lvalues" : what does it mean?


It means:

#define N 100 /* or some other constant */

struct A {
char arr[N];
};

struct A f(void);

void example(void) {
char c;

c = f().arr[10];
}

is now valid (it was not valid in C89). The function f() returns
an "rvalue" (or more simply, a "value"), not an "lvalue" (something
that designates, or at least potentially designates, an object).

In most C code, the only time you encounter an array, it is an
array object -- an lvalue -- as in:

char buf[30];
...
buf[10] = 'x';

Here "buf" names the entire array, but the array is an object --
a region of data storage; in this case it is the array named "buf"
-- and the <object, array N of T, buf> triple is converted to a
<value, pointer to T, &buf[0]> triple. That is, the "lvalue" that
names the entire array is converted to an "rvalue" (i.e., an ordinary
value) that points to the first element of the array.

In C89, only lvalue-arrays were converted to rvalue-pointers.
Since rvalue-arrays are relatively rare -- they arise only from
function calls like the one above, and some other rarely-encountered
expressions -- one tends not to see this in real C code.

(The other reason one tends not to find this in "real" or "working"
C code is that compilers given source code like this will, at least
relatively often, produce object code that does not work correctly.
Even in C99, it is somewhat hard to use this conversion safely.
Consider, for instance:

void bad_example(void) {
char *p;

p = f().arr;
printf("[0] = %c\n", p[0]);
printf("[4] = %c\n", p[4]);
}

Depending on how the call is implemented, p[0] and p[4] may well
no longer exist by the time the printf() calls are made. The first
printf() may print the desired value, but the call to printf() may
change the byte accessed by the second call, so that the second
printf() prints something other than what the programmer expected.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: gmail (figure it out) http://web.torek.net/torek/index.html

nicolas.sitbon@gmail.com 08-04-2008 07:39 PM

Re: conversion of array to pointer not limited to lvalues
 
I'm sory but I don't understand the difference between a lvalue-arrays
and a rvalue-arrays?
Are you saying that
struct A
{
char arr[N];
};

and
char arr2[N];
are conceptually different?

Barry Schwarz 08-04-2008 07:48 PM

Re: conversion of array to pointer not limited to lvalues
 
On Mon, 4 Aug 2008 10:44:05 -0700 (PDT), nicolas.sitbon@gmail.com
wrote:

>hi everybody, In the new feature of C99, i find " conversion of array
>to pointer not limited to lvalues" : what does it mean? I don't find
>any difference between C90 and C99 about conversion of array to
>pointer.


You didn't look carefully enough, though I don't know what the real
impact of the wording change is.

In C89, para 3.2.2.1 says "Except when ..., an lvalue that has
type ``array of type '' is converted ..."

In C99, para 6.3.2.1-3 say "Except when ..., an expression that has
type array of type is converted ..."

In the new wording, the expression need not be an lvalue. Maybe
someone who worked on the standard can explain how this affects the
average programmer or if it is something only a compiler writer would
care about.

--
Remove del for email

Harald van Dijk 08-04-2008 07:56 PM

Re: conversion of array to pointer not limited to lvalues
 
On Mon, 04 Aug 2008 12:39:45 -0700, nicolas.sitbon wrote:
> I'm sory but I don't understand the difference between a lvalue-arrays
> and a rvalue-arrays?
> Are you saying that
> struct A
> {
> char arr[N];
> };
>
> and
> char arr2[N];
> are conceptually different?


Not directly, as far as the array is concerned. However, the former is an
allowable function return type, while the latter is not.

santosh 08-04-2008 07:56 PM

Re: conversion of array to pointer not limited to lvalues
 
nicolas.sitbon@gmail.com wrote:

> I'm sory but I don't understand the difference between a lvalue-arrays
> and a rvalue-arrays?
> Are you saying that
> struct A
> {
> char arr[N];
> };
>
> and
> char arr2[N];
> are conceptually different?


Aren't they? They are different types. The first one is type struct A
and the second one is type char [N].

But the point is wrapping an array within a structure allows one to
return an array from functions, which cannot return plain arrays.
However the manner in which it is accessed is different from how you
would normally access an array, and this has been standardised by C99.
Since functions return values, you must store the entire structure (or
at least it's array member) in another object or you will lose it.

Please see Chris Torek's post. You can't get a better explanation than
that.


nicolas.sitbon@gmail.com 08-04-2008 07:59 PM

Re: conversion of array to pointer not limited to lvalues
 
On 4 août, 21:56, Harald van Dijk <true...@gmail.com> wrote:
> On Mon, 04 Aug 2008 12:39:45 -0700, nicolas.sitbon wrote:
> > I'm sory but I don't understand the difference between a lvalue-arrays
> > and a rvalue-arrays?
> > Are you saying that
> > struct A
> > {
> > * *char arr[N];
> > };

>
> > and
> > char arr2[N];
> > are conceptually different?

>
> Not directly, as far as the array is concerned. However, the former is an
> allowable function return type, while the latter is not.


the structure is returned, not the table directly.

santosh 08-04-2008 08:06 PM

Re: conversion of array to pointer not limited to lvalues
 
nicolas.sitbon@gmail.com wrote:

> On 4 aot, 21:56, Harald van D?k <true...@gmail.com> wrote:
>> On Mon, 04 Aug 2008 12:39:45 -0700, nicolas.sitbon wrote:
>> > I'm sory but I don't understand the difference between a
>> > lvalue-arrays and a rvalue-arrays?
>> > Are you saying that
>> > struct A
>> > {
>> > char arr[N];
>> > };

>>
>> > and
>> > char arr2[N];
>> > are conceptually different?

>>
>> Not directly, as far as the array is concerned. However, the former
>> is an allowable function return type, while the latter is not.

>
> the structure is returned, not the table directly.


A value of type struct A is returned which contains an array member of
type char [n]. You can do this too:

struct A ret = foo(); /* returns a struct A value */
printf("%c\n", ret.arr[0]);

instead of

printf("%c\n", foo().arr[0]);


nicolas.sitbon@gmail.com 08-04-2008 08:10 PM

Re: conversion of array to pointer not limited to lvalues
 
On 4 aot, 22:06, santosh <santosh....@gmail.com> wrote:
> nicolas.sit...@gmail.com wrote:
> > On 4 aot, 21:56, Harald van D?k <true...@gmail.com> wrote:
> >> On Mon, 04 Aug 2008 12:39:45 -0700, nicolas.sitbon wrote:
> >> > I'm sory but I don't understand the difference between a
> >> > lvalue-arrays and a rvalue-arrays?
> >> > Are you saying that
> >> > struct A
> >> > {
> >> > char arr[N];
> >> > };

>
> >> > and
> >> > char arr2[N];
> >> > are conceptually different?

>
> >> Not directly, as far as the array is concerned. However, the former
> >> is an allowable function return type, while the latter is not.

>
> > the structure is returned, not the table directly.

>
> A value of type struct A is returned which contains an array member of
> type char [n]. You can do this too:
>
> *struct A ret = foo(); /* returns a struct A value */
> *printf("%c\n", ret.arr[0]);
>
> instead of
>
> *printf("%c\n", foo().arr[0]);


OK, I know that, but except the fact that we can return the table
(which is in a structure), what's the difference between the two table?

Barry Schwarz 08-04-2008 09:01 PM

Re: conversion of array to pointer not limited to lvalues
 
On Mon, 4 Aug 2008 12:39:45 -0700 (PDT), nicolas.sitbon@gmail.com
wrote:

>I'm sory but I don't understand the difference between a lvalue-arrays
>and a rvalue-arrays?
>Are you saying that
>struct A
>{
> char arr[N];
>};
>
>and
>char arr2[N];
>are conceptually different?


I don't understand many facets of lvalue and rvalue either but one
obvious difference is that your struct is only a declaration of a type
while your arr2 is the definition of an object. If you define a
struct A named x, there is still one obvious difference. Passing x to
a function will result in a complete copy of the array arr being
passed to the function. Passing arr2 to a similar function will
result in the address of the array being passed. In the first case,
updates to array elements are local to the called function. In the
second, updates are made directly to the array in the calling
function.

--
Remove del for email


All times are GMT. The time now is 03:31 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.