Topic: string literals - lvalues ?


Author: krotoff@boy.rector.msu.su (Alexsander Krotoff)
Date: 1995/09/05
Raw View
Steve Clamage (clamage@Eng.Sun.COM) wrote:
: krotoff@boy.rector.msu.su (Alexsander Krotoff) writes:

: >I have a questio: is it mentioned in the c++ draft standard are
: >string literals lvalues or not ? Is it possible to take an address
: >of string ?

: A string literal represents a null-terminated array of characters
: with static storage duration. When you write
:  "abc"
: in an expression (except as the operand of sizeof), it is
: immediately converted to the address of the first char in the array
: which consists of 'a', 'b', 'c', '\0'.

: You cannot write &"abc", because that would be the address of an
: address, and has no meaning.

Thank you for respose.

The same rule is in the C, but in C it is legal to write:
char (*ca)[4] = &"abc";
since string is lvalue and the only requirement for `&' operand is
function designator or an lvalue that designates an object.
(ANSI C 3.3.3.2 "Address and indirection operators").

I am missing something ?
--
Alexander Krotoff    <krotoff@such.srcc.msu.su>
Research Computer Center   [Moscow]939-2638
Moscow State University    MGU, SRCC k316. GZ B-733r.


---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: CS-Usenet <news@cs.mu.OZ.AU>
Date: 1995/09/05
Raw View
krotoff@boy.rector.msu.su (Alexsander Krotoff) writes:

>I have a questio: is it mentioned in the c++ draft standard are
>string literals lvalues or not ? Is it possible to take an address
>of string ?

No, the current C++ draft does not specify this. :-(
However, the C standard does specify that string literals are lvalues
and hence that their address can be taken, and it is pretty likely
that the eventual C++ standard will say the same thing.

--
Fergus Henderson             |  #define x t=a[i],a[i]=a[m],a[m]=t
                             |  char a[]=" 12345678";main(m,i,j,t){for(i=m;i<
fjh@cs.mu.oz.au              |  9;x,i++)for(x,j=m;--j?(t=a[m-j]-a[m])-j&&t+j:
http://www.cs.mu.oz.au/~fjh  |  main(m+1)*0;);m-9||puts(a+1);} /* 8 queens */


---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: krotoff@boy.rector.msu.su (Alexsander Krotoff)
Date: 1995/09/01
Raw View
Hello gurus,

I have a questio: is it mentioned in the c++ draft standard are
string literals lvalues or not ? Is it possible to take an address
of string ?

ANSI C (3.3.1 Primary expressions)
 A string literal is a primary expression. It is an lvalue
 with type defined in 3.1.4.

C++ DS 3.9 (Lvalues and rvalues)
 An lvalue refers to an object or function.

I did not found to find explicit answer.

Thank you.
--
Alexander Krotoff    <krotoff@such.srcc.msu.su>
Research Computer Center   [Moscow]939-2638
Moscow State University    MGU, SRCC k316. GZ B-733r.
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/09/02
Raw View
krotoff@boy.rector.msu.su (Alexsander Krotoff) writes:

>I have a questio: is it mentioned in the c++ draft standard are
>string literals lvalues or not ? Is it possible to take an address
>of string ?

A string literal represents a null-terminated array of characters
with static storage duration. When you write
 "abc"
in an expression (except as the operand of sizeof), it is
immediately converted to the address of the first char in the array
which consists of 'a', 'b', 'c', '\0'.

You cannot write &"abc", because that would be the address of an
address, and has no meaning.
--
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: 1995/09/02
Raw View
clamage@Eng.Sun.COM (Steve Clamage) writes:

>krotoff@boy.rector.msu.su (Alexsander Krotoff) writes:
>
>>I have a questio: is it mentioned in the c++ draft standard are
>>string literals lvalues or not ? Is it possible to take an address
>>of string ?
>
>A string literal represents a null-terminated array of characters
>with static storage duration.

I agree with you so far.

>When you write
> "abc"
>in an expression (except as the operand of sizeof), it is
>immediately converted to the address of the first char in the array
>which consists of 'a', 'b', 'c', '\0'.

Here I disagree.  I think you should add "(and except as the operand of
unary &)" as another exception.

What makes you believe that array-to-pointer conversion will apply to
the operand of address-of operator?  Can you back this up with quotes
from either the C standard or the draft C++ standard?
IMHO, your belief is wrong, as the following code clearly demonstrates:

 int array[4];
 int (*pointer_to_array) [4] = &array;

This code is legal C and C++.  The C++ draft [section 4, paragraph 4],
says the following (the capitals for emphasis are mine):

| 4 One  or  more of the following standard conversions will be applied to
|   an expression IF NECESSARY to convert it  to  a  required  destination
|   type.

In the case where the operand of the address-of operator is an lvalue
of array type, no such conversion is necessary and therefore no
such conversion is performed.

The doubt about whether or not &"abc" is legal is due to the fact
that the draft C++ standard doesn't specify whether a string literal
is an lvalue or not.  Since the C standard does specify this - it
specifies that a string literal *is* an lvalue - I would expect the
eventual C++ standard to follow the C standard.  &"abc" is legal C,
and I expect it will be legal C++.

(Mind you, I would not be suprised if some C++ compilers were to still get
this wrong years after the C++ standard gets published, so when writing
portable code I'd try to avoid this construct.)

--
Fergus Henderson             |  #define x t=a[i],a[i]=a[m],a[m]=t
                             |  char a[]=" 12345678";main(m,i,j,t){for(i=m;i<
fjh@cs.mu.oz.au              |  9;x,i++)for(x,j=m;--j?(t=a[m-j]-a[m])-j&&t+j:
http://www.cs.mu.oz.au/~fjh  |  main(m+1)*0;);m-9||puts(a+1);} /* 8 queens */
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]