Please see the code below :-
void func()
{
unsigned char x,y,z=1;
(z==1) ? (x) : (y) = 1; /* Compiles OK */
((z==1) ? (x) : (y)) = 1; /* Compiler generates an error "Variable
expected" */
}
Its just an example, I'd mentioned above. I am wondering why does
putting a parenthesis around the second expression is erreneous. I
tried above peice of code with three different compiler, one of the
compiler GCC 2.96 compiles it ok. However an older version of GCC and
Microsoft VC++ compilers generate an error "variable expected". I'd
like to know, are such assignment(s) allowed ? If yes, why the first
assignment compiles ok whereas second one fails.
Thanks in advance.
Mahesh 6 3646
On Mon, 21 Jul 2003, Mahesh Tomar wrote: void func() { unsigned char x,y,z=1; (z==1) ? (x) : (y) = 1; /* Compiles OK */
Right. If z==1, then x is evaluated and its value discarded.
If z!=1, then 1 is assigned to y.
((z==1) ? (x) : (y)) = 1; /* Compiler generates an error "Variable expected" */
Right. If z==1, then x is evaluated.
If z!=1, then y is evaluated.
Then you have an '= 1' tacked onto the end. To what does
this assign? It can't assign to the value of x, or the value
of y; that's just as silly as writing
-x = 1; /* Assign to the value of -x ? */
The result of the ?: operator is no more an lvalue than the result
of the - operator, or the = operator itself.
Its just an example, I'd mentioned above. I am wondering why does putting a parenthesis around the second expression is erreneous.
For the same reason that putting parentheses around the (1,2) in
the following expression is "erroneous":
atan2(1,2) -- OK
atan2((1,2)) -- invalid; only one argument to atan2()
You're adding random parentheses in an attempt to make C do something
that it can't.
I tried above peice of code with three different compiler, one of the compiler GCC 2.96 compiles it ok. However an older version of GCC and Microsoft VC++ compilers generate an error "variable expected". I'd like to know, are such assignment(s) allowed ?
The result of ?: is *not* an lvalue, and thus can't be assigned to.
The first statement doesn't do what you think it does.
-Arthur
Mahesh Tomar wrote: Please see the code below :-
void func() { unsigned char x,y,z=1; (z==1) ? (x) : (y) = 1; /* Compiles OK */ ((z==1) ? (x) : (y)) = 1; /* Compiler generates an error "Variable expected" */ }
Its just an example, I'd mentioned above. I am wondering why does putting a parenthesis around the second expression is erreneous. I tried above peice of code with three different compiler, one of the compiler GCC 2.96 compiles it ok. However an older version of GCC and Microsoft VC++ compilers generate an error "variable expected". I'd like to know, are such assignment(s) allowed ? If yes, why the first assignment compiles ok whereas second one fails.
Both lines are erroneous. If either compiles without
a diagnostic[*], the compiler is faulty.
[*] Strictly speaking, the compiler is not required to
issue a separate diagnostic for each line. However, most
will attempt to do so.
-- Er*********@sun.com
Eric Bernard <ew***@sympatico.ca> wrote:
[please don't top-post in technical newsgroups] "Mahesh Tomar" <to*****@msn.com> wrote in message news:46**************************@posting.google.c om... Please see the code below :-
void func() { unsigned char x,y,z=1; (z==1) ? (x) : (y) = 1; /* Compiles OK */ ((z==1) ? (x) : (y)) = 1; /* Compiler generates an error "Variable expected" */ }
[...] The ?: operator returns a right-value.
This is true.
You cannot use it to choose which variable to assign to.
This is not true - for example, we can rewrite the OPs code:
void func()
{
unsigned char x, y, z = 1;
*((z == 1) ? &x : &y) = 1;
}
- Kevin.
"Kevin Easton" <kevin@-nospam-pcug.org.au> wrote in message
news:ne********************@tomato.pcug.org.au... Eric Bernard <ew***@sympatico.ca> wrote: [please don't top-post in technical newsgroups] "Mahesh Tomar" <to*****@msn.com> wrote in message news:46**************************@posting.google.c om... Please see the code below :-
void func() { unsigned char x,y,z=1; (z==1) ? (x) : (y) = 1; /* Compiles OK */ ((z==1) ? (x) : (y)) = 1; /* Compiler generates an error "Variable expected" */ } [...] The ?: operator returns a right-value.
This is true.
You cannot use it to choose which variable to assign to.
This is not true - for example, we can rewrite the OPs code:
void func() { unsigned char x, y, z = 1; *((z == 1) ? &x : &y) = 1; }
- Kevin.
Right, you *can* do that, but would you?
A simple if(z==1) x=1; else y=1; looks a bit nicer. At least to me.
Eric Sosman <Er*********@sun.com> wrote: Mahesh Tomar wrote: Please see the code below :-
void func() { unsigned char x,y,z=1; (z==1) ? (x) : (y) = 1; /* Compiles OK */ ((z==1) ? (x) : (y)) = 1; /* Compiler generates an error "Variable expected" */ }
Both lines are erroneous. If either compiles without a diagnostic[*], the compiler is faulty.
What's wrong with the first line? Yes, it invokes undefined behaviour
because x is uninitialised, but imprimis that's a fault in the whole
function, not that one line, and secundis diagnostics are not required
for UB.
Richard
Richard Bos wrote: Eric Sosman <Er*********@sun.com> wrote:
Mahesh Tomar wrote: Please see the code below :-
void func() { unsigned char x,y,z=1; (z==1) ? (x) : (y) = 1; /* Compiles OK */ ((z==1) ? (x) : (y)) = 1; /* Compiler generates an error "Variable expected" */ }
Both lines are erroneous. If either compiles without a diagnostic[*], the compiler is faulty.
What's wrong with the first line? [...]
Unless I'm wrong (it's happened ...), both parse identically:
the `?:' ternary operator "binds more tightly than" the `='
assignment operator. Let's see if I can get there from the
grammar:
- (z==1) is recognized as the "logical-OR-expression"
constituting the first operand of `?:'
- (x) is recognized as the "expression" constituting
the second operand
- (y) is recognized as the "conditional-expression"
constituting the third operand
- ... so the whole business before the `=' is recognized
as a "conditional-expression"
- ... which isn't an lvalue, as required by the `='
operator.
FWIW, the two completely different compilers I tried it
on both issue diagnostics for the line in question, although
gcc accepts *both* lines when run without `-ansi'.
-- Er*********@sun.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: TheKeith |
last post by:
I just wrote the following script for something I'm working on:
----------------------------------------------------------------------------
-------------------
<html>
<head>
<script...
|
by: Ney André de Mello Zunino |
last post by:
Hello.
I have noticed, in a lot of C and C++ code, that many programmers seem
to prefer putting the test values first in conditional expressions.
I.e., they would rather write this:
if (-1 ==...
|
by: mux |
last post by:
Hi
I found out that the following piece of code throws an error.
1 #include "stdio.h"
2
3 int main()
4 {
5 int a,b;
6 a= 10;
|
by: Chris Dunaway |
last post by:
Consider this code (.Net 2.0) which uses a nullable type:
private void button1_Click(object sender, System.EventArgs e)
{
DateTime? nullableDate;
nullableDate = (condition) ? null :...
|
by: Marty |
last post by:
Hi,
Does using the the conditional operator (?:) instead of the common "if"
statement will give a performance gain in a C# .NET 2003 application
(even in C# .NET 2005?).
What is the advantage...
|
by: paulo |
last post by:
Can anyone please tell me how the C language interprets the following
code:
#include <stdio.h>
int main(void)
{
int a = 1;
int b = 10;
int x = 3;
|
by: Nicholas M. Makin |
last post by:
I was just thinking that I understood the conditional operator when I coded
the following expecting it to fail:
int a= 10, b= 20, c= 0;
((a < b) ? a : b) = c; // a=0
a=20; b= 10;
((a < b) ? a...
|
by: somenath |
last post by:
Hi All,
I have one question regarding the conditional operator.
In the draft C99 standard it is mentioned that
"1 The following are the sequence points described in 5.1.2.3:
-- The call to a...
|
by: Neal Becker |
last post by:
In hindsight, I am disappointed with the choice of conditional syntax. I know it's too late to change. The problem is
y = some thing or other if x else something_else
When scanning this my...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |