Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is the expression 1 ? "123" : "1234" a valid one ?

Reply
Thread Tools

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

 
 
邓尧
Guest
Posts: n/a
 
      04-24-2012
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
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      04-24-2012
邓尧 <(E-Mail Removed)> writes:

> Hi, everyone
>
> Is the following expression a valid one ?
> 1 ? "123" : "1234"


Yes it is.

<snip>
> 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:
>
>> 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;
>> — 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.


> 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[...]"

--
Ben.
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      04-24-2012
On 4/24/2012 7:20 AM, 邓尧 wrote:
> 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:
>
>> The first operand shall have scalar type.


It does.

>> 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;


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 `&'.)

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      04-24-2012
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.
--
James Kuyper
 
Reply With Quote
 
邓尧
Guest
Posts: n/a
 
      04-24-2012
On Tuesday, April 24, 2012 7:55:14 PM UTC+8, James Kuyper wrote:
> 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.
> --
> James Kuyper


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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-25-2012
Eric Sosman <(E-Mail Removed)> writes:
[...]
> (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.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      04-25-2012
On 4/24/2012 9:38 PM, Keith Thompson wrote:
> Eric Sosman<(E-Mail Removed)> writes:
> [...]
>> (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.)

--
Eric Sosman
(E-Mail Removed)d
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Valid regular expression not working with validator control Andrew Jocelyn ASP .Net 3 03-05-2008 11:44 AM
RegularExpressionValidator generates a &nbsp; if its a valid expression. Eric Layman ASP .Net 2 03-28-2007 01:05 AM
Regular expression for identifiying a valid street name + house number Ole Perl 0 07-31-2003 09:50 AM



Advertisments