É
é‚“å°§
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:
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.
Any ideas ?
Thanks
Yao
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