Thread View: comp.std.c++
6 messages
6 total messages
Started by Alexsander Krotoff
Fri, 01 Sep 1995 18:13
string literals - lvalues ?
Author: Alexsander Krotoff
Date: Fri, 01 Sep 1995 18:13
Date: Fri, 01 Sep 1995 18:13
24 lines
1275 bytes
1275 bytes
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. ]
Re: string literals - lvalues ?
Author: Steve Clamage
Date: Sat, 02 Sep 1995 05:52
Date: Sat, 02 Sep 1995 05:52
21 lines
1337 bytes
1337 bytes
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. ]
Re: string literals - lvalues ?
Author: Fergus Henderson
Date: Sat, 02 Sep 1995 08:43
Date: Sat, 02 Sep 1995 08:43
61 lines
3007 bytes
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. ]
Re: string literals - lvalues ?
Author: Tom Payne
Date: Mon, 04 Sep 1995 15:36
Date: Mon, 04 Sep 1995 15:36
30 lines
1763 bytes
1763 bytes
Fergus Henderson (fjh@munta.cs.mu.OZ.AU) wrote:
: 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++.
If "abc" isn't an lvalue, then sizeof("abc") is also illegal -- there
are no rvalues of type char-array, and array-to-pointer conversion is
supressed in this context, per 5.3.3:
4 The lvalue-to-rvalue, array-to-pointer, and
function-to-pointer standard conversions are
suppressed on the operand of sizeof.
Visibly, string literals must be lvalues for compatibility with C.
Tom Payne (thp@cs.ucr.edu)
[ 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. ]
Re: string literals - lvalues ?
Author: CS-Usenet
Date: Tue, 05 Sep 1995 05:00
Date: Tue, 05 Sep 1995 05:00
23 lines
1447 bytes
1447 bytes
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. ]
Re: string literals - lvalues ?
Author: Alexsander Krotoff
Date: Tue, 05 Sep 1995 05:06
Date: Tue, 05 Sep 1995 05:06
37 lines
1865 bytes
1865 bytes
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. ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads