🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1892

Re: string literals - lvalues ?

#1892
From: Fergus Henderson
Date: Sat, 02 Sep 1995 08:43
61 lines
3007 bytes
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. ]
Technical Details
Message-ID: <9524518.21198@mulga.cs.mu.OZ.AU>
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
References: <427hu8$2k8@alpha.phys.msu.su> <428qq7$jlp@engnews2.Eng.Sun.COM>

Show Full Headers
Complete Article Headers
Approved: fjh@cs.mu.oz.au
Date: 02 Sep 95 08:43:33 GMT
From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Message-ID: <9524518.21198@mulga.cs.mu.OZ.AU>
Newsgroups: comp.std.c++
Organization: Computer Science, University of Melbourne, Australia
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
References: <427hu8$2k8@alpha.phys.msu.su> <428qq7$jlp@engnews2.Eng.Sun.COM>
Subject: Re: string literals - lvalues ?