This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: string is const char [] ?
- To: <egcs at cygnus dot com>, <egcs-bugs at cygnus dot com>
- Subject: Re: string is const char [] ?
- From: "Ross Smith" <ross dot s at ihug dot co dot nz>
- Date: Thu, 25 Jun 1998 14:40:52 +1200
From: Kamil Iskra <kamil@dwd.interkom.pl>
>
> char a1[] = "test not const";
>
>I didn't want a const array, I wanted a plain one, probably for a good
>reason. Maybe I wanted to change some characters or append something to
>it? Now, how should I do that to satisfy the standard and avoid the
much
>more error-prone standard array-initialisation syntax?
>
>Don't get me wrong, I'm sure you know the standard far better than I
do,
>but are you SURE that it disallows this initialisation syntax? I find
it
>hard to believe, since first, I think this syntax is very common (at
least
>I use it quite often), and second, this syntax is fine in C, and C++ is
>generally supposed to accept C code (I know, there are certain
>incompatibilities, and, IIRC, there is even a list of them. Is this
>particular incompatibility listed there?).
>
>If the standard indeed does deliberately disallow this initialisation
>syntax, I would vote that G++ allows it, preferably without any
warnings
>(unless compiling with -ansi, -pedantic or some such).
The standard *does* allow the above syntax. (Or at least CD2 did; I
haven't seen the FDIS but I haven't heard about any change in this
area.) The relevant section is 8.5.2 [dcl.init.string]. It makes a
special exception to the normal initialisation rules, allowing the
initialisation of a non-const char (or wchar_t) array with a string
literal.
(There is one change from the old C rules, though: if you specify the
array size, you can't leave out the terminating null. E.g. char foo[3] =
"bar"; is legal in C (giving an unterminated string) but illegal in
C++.)
--
Ross Smith ................................... mailto:ross.s@ihug.co.nz
.............. The Internet Group, Auckland, New Zealand ..............
"Remember when we told you there was no future? Well, this is it."
-- Blank Reg