Is the expression 1 ? "123" : "1234" a valid one ?

É

é‚“å°§

Hi, everyone

Is the following expression a valid one ?
1 ? "123" : "1234"

I'm not sure about the answer, but many C compilers (MSVC/GCC/Clang) support this expression.

Firstly, string literals are of type character array. According to C specification draft n1124 section 6.5.15, conditional operator has to satisfy thefollowing constraints:
The first operand shall have scalar type.
One of the following shall hold for the second and third operands:
— both operands have arithmetic type;
— both operands have the same structure or union type;
— both operands have void type;
— both operands are pointers to qualified or unqualified versions of compatible types;

I'm not sure where this condition is held for the expression. If for some reason the two string literals are converted to two pointers, the expressionis a valid one, otherwise it's invalid.
— one operand is a pointer and the other is a null pointer constant; or
— one operand is a pointer to an object or incomplete type and the other is a pointer to a qualified or unqualified version of void.

Any ideas ?

Thanks
Yao
 
B

Ben Bacarisse

é‚“å°§ said:
Hi, everyone

Is the following expression a valid one ?
1 ? "123" : "1234"

Yes it is.

Firstly, string literals are of type character array. According to C
specification draft n1124 section 6.5.15, conditional operator has to
satisfy the following constraints:
I'm not sure where this condition is held for the expression. If for
some reason the two string literals are converted to two pointers, the
expression is a valid one, otherwise it's invalid.

The "for some reason" is 6.3.2.1 paragraph 3:

"Except when it is the operand of the sizeof operator or the unary &
operator, or is a string literal used to initialize an array, an
expression that has type 'array of type' is converted to an
expression with type 'pointer to type' that points to the initial
element of the array object[...]"
 
E

Eric Sosman

Hi, everyone

Is the following expression a valid one ?
1 ? "123" : "1234"
Yes.

I'm not sure about the answer, but many C compilers (MSVC/GCC/Clang) support this expression.

Firstly, string literals are of type character array. According to C specification draft n1124 section 6.5.15, conditional operator has to satisfy the following constraints:

It does.

They are. When an array reference appears in an expression, it
almost always means "pointer to the array's [0] element" (there are
two exceptions that do not apply here). "123" generates a nameless
array of char, and mentioning it produces a char* pointer value that
aims at the '1'. Similarly, "1234" generates a nameless array and
produces a char* pointer to its '1'. With this in mind, your
expression can be analyzed as

1 ? "123" : "1234"
| | |
| | |
V V V
(int) ? (char[4]) : {char[5])
| | |
| | |
V V V
(int) ? (char*) : (char*)

.... and the last two operands are indeed "pointers ... of compatible
types."

(For the record, the two exceptions to "array references produce
pointers in expressions" rule are: When the array is the operand of
`sizeof', and when it is the operand of the address-of operator `&'.)
 
J

James Kuyper

On 04/24/2012 07:20 AM, é‚“å°§ wrote:
....
... According to C specification draft n1124 section 6.5.15, ...

Other people have already answered your question, so I'll just address a
side issue. The current version of the C2011 standard is not greatly
different from draft n1570; C99 is most accurately represented by draft
n1256 (oddly enough, it's actually more complete and up-to-date than any
official version of C99). I don't think there's any reason to rely upon
n1124.
 
É

é‚“å°§

On 04/24/2012 07:20 AM, é‚“å°§ wrote:
...

Other people have already answered your question, so I'll just address a
side issue. The current version of the C2011 standard is not greatly
different from draft n1570; C99 is most accurately represented by draft
n1256 (oddly enough, it's actually more complete and up-to-date than any
official version of C99). I don't think there's any reason to rely upon
n1124.

Thanks for the information. I picked up n1124, because google "c99 specification" showed it as the first result, I should have been more careful :-(

Thanks for all the replies
Yao
 
K

Keith Thompson

Eric Sosman said:
(For the record, the two exceptions to "array references produce
pointers in expressions" rule are: When the array is the operand of
`sizeof', and when it is the operand of the address-of operator `&'.)

And the third is when it's a string literal in an initializer used to
initialize an array object. Example: In

char s[6] = "hello";

the literal "hello" doesn't decay to a pointer; the array value is
copied into s.
 
E

Eric Sosman

Eric Sosman said:
(For the record, the two exceptions to "array references produce
pointers in expressions" rule are: When the array is the operand of
`sizeof', and when it is the operand of the address-of operator `&'.)

And the third is when it's a string literal in an initializer used to
initialize an array object. Example: In

char s[6] = "hello";

the literal "hello" doesn't decay to a pointer; the array value is
copied into s.

I had intended "in expressions" to rule out initializers and
splices of adjacent literals, but "in expressions" isn't quite
good enough: Many initializers are expressions, and (I think) all
initializers contain expressions. Thanks for the correction.

Perhaps I should have said something like "array references as
operands produce pointers, with two exceptions." (Keith knows this,
but in case others don't: The `=' in his example is not an assignment
operator, but a separator required by the syntax of initialization.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top