I agree, but the authors of the HP aCC compiler and a bug reported
against GNU C compiler disagrees. See
<URL:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11250> for the GCC
bug report. I'm trying to find out if they are correct or not.
The HP aCC compiler reject the code with an error, and a future GCC
will issue a warning when using -pedantic.
I guess I need a good ANSI C lawyer. Anyone around?
ISO 9899-1990 6.5.7 ("Initialization") says "An array of character
type may be initialized by a character string literal, optionally
enclosed in braces". (As I and others have noted in other posts, the
C standard uses "braces" for the "{}" characters, and no others.)
That looks pretty clear. There's a special rule for initializing
arrays of character type with string literals or string literals
enclosed with braces. Not with expressions that evaluate to string
literals - string literals themselves. (Actually, I don't believe a
parenthesized string literal is an expression that evaluates to a
string literal - it decays to a pointer to its first element, doesn't
it? See below.)
Note that this is a special restriction added by the Semantics
section of 6.5.7. It doesn't follow from the Syntax section, which
gives:
initializer:
assignment-expression
{ initializer-list }
{ initializer-list , }
assignment-expression is specified in 6.3.16:
assignment-expression:
conditional-expression
unary-expression assignment-operator assignment-expression
(That is, it's the syntax rule that describes "chaining" assignments
like "a = b = 0;".)
A conditional-expression can be a logical-OR-expression, which can be
a logical-AND-expression, and so on, until you get to primary-
expression, which includes parenthesized expressions and string
literals as separate productions:
primary-expression:
identifer
constant
string-literal
( expression )
Since expression can be an assignment-expression, though, a parenthe-
sized string literal is still a valid primary expression, according
to the grammar.
This is actually relatively common in the C standard - the phrase
structure grammar is much more liberal than what the language
actually permits, and much of the text is devoted to listing cases
where phrases allowed by the grammar as specified are not in fact
part of the language.
Curiously, it should be legal to write:
char *cp = ("foo");
even though
char s[] = ("foo");
is not. That's because the former is a normal scalar initializer;
the string literal decays into a pointer to its first element, and
the value of that pointer is assigned to cp. The latter is an
attempt to initialze an array of char with a pointer to char, so it
fails, just as
char *cp = ("foo");
char s[] = cp;
would.
--
Michael Wojcik (e-mail address removed)
She felt increasingly (vision or nightmare?) that, though people are
important, the relations between them are not, and that in particular
too much fuss has been made over marriage; centuries of carnal
embracement, yet man is no nearer to understanding man. -- E M Forster