Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

?: as an lvalue
Hi Everyone,

I have the following piece of code, and i expected an error, however
i don't get an error,

int main()
{
int aa=0,b=0;
1>0?aa:b = 10;
printf("value %d %d\n",aa,b);
return(0);
}

and output is value 0 0

Thanks in advance ! ! !

Report this thread to moderator Post Follow-up to this message
Old Post
Rahul
03-30-08 01:00 PM


Re: ?: as an lvalue
In article <f840856d-216c-4bab-ac4a-f962f88a2c3a@h11g2000prf.googlegroups.co
m>,
Rahul  <sam_cit@yahoo.co.in> wrote:
>Hi Everyone,
>
> I have the following piece of code, and i expected an error, however
>i don't get an error,
>
>int main()
>{
>	int aa=0,b=0;
>	1>0?aa:b = 10;
>        printf("value %d %d\n",aa,b);
> return(0);
>}
>
>and output is value 0 0
>
>Thanks in advance ! ! !

int main(void) is better
failure to include stdio.h
unnecessarily parenthesizing the return value
failure to indent the return statement properly
meaningless variable names
failure to capitalize "i" in text (e.g., "i don't get an error")
excessive use of exclamation points
improper capitalization of word "everyone"
use of tacky expression "Thanks in advance"


Report this thread to moderator Post Follow-up to this message
Old Post
Kenny McCormack
03-30-08 01:00 PM


Re: ?: as an lvalue
Rahul said:

> Hi Everyone,
>
>  I have the following piece of code, and i expected an error, however
> i don't get an error,
>
> int main()
> {
> int aa=0,b=0;
> 1>0?aa:b = 10;

This is a syntax error (which therefore requires the implementation to
diagnose it as such). The syntax of the conditional operator is:

conditional-expression:
logical-OR-expression
logical-OR-expression ? expression : conditional-expression

which your code violates.

>         printf("value %d %d\n",aa,b);
>  return(0);
> }
>
> and output is value 0 0
>
> Thanks in advance ! ! !

Turn up your warning level. Which implementation are you using?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Report this thread to moderator Post Follow-up to this message
Old Post
Richard Heathfield
03-30-08 01:00 PM


Re: ?: as an lvalue
Richard Heathfield <rjh@see.sig.invalid> writes:

> Rahul said:
> 
>
> This is a syntax error (which therefore requires the implementation to
> diagnose it as such). The syntax of the conditional operator is:
>
>   conditional-expression:
>     logical-OR-expression
>     logical-OR-expression ? expression : conditional-expression
>
> which your code violates.

That is not really enough to show there is a syntax error.  The
syntax for an assignment-expression

assignment-expression:
conditional-expression
unary-expression assignment-operator assignment-expression

is more suggestive, but even then, the OP needs to know what might or
might not constitute a unary-expression.  It is an interesting problem
to find the smallest set of grammar rules needed to show that any
given piece of C is a syntax error.

--
Ben.

Report this thread to moderator Post Follow-up to this message
Old Post
Ben Bacarisse
03-31-08 12:10 AM


Re: ?: as an lvalue
On Mar 30, 5:19 pm, Richard Heathfield <r...@see.sig.invalid> wrote:
> Rahul said:
> 
> 
> 
>
> This is a syntax error (which therefore requires the implementation to
> diagnose it as such). The syntax of the conditional operator is:
>
>   conditional-expression:
>     logical-OR-expression
>     logical-OR-expression ? expression : conditional-expression
>
> which your code violates.
> 
> 
> 
>
> Turn up your warning level. Which implementation are you using?
>
> --
> Richard Heathfield <http://www.cpax.org.uk>
> Email: -http://www. +rjh@
> Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
> "Usenet is a strange place" - dmr 29 July 1999

I tried MS VC++ 8.0...
http://www.dinkumware.com/exam/default.aspx

Report this thread to moderator Post Follow-up to this message
Old Post
Rahul
03-31-08 12:10 AM


Re: ?: as an lvalue
Rahul wrote, On 30/03/08 12:05:
> Hi Everyone,
>
>  I have the following piece of code, and i expected an error, however
> i don't get an error,

In that case you need to tell your compiler to act as a C compiler
rather than as an extended-C-like-language compiler.

> int main()
> {
> 	int aa=0,b=0;
> 	1>0?aa:b = 10;

This is illegal and a C compiler is required to produce a diagnostic
(warning, error or whatever).

>         printf("value %d %d\n",aa,b);

Not related to your current problem, but you need to include stdio.h to
use printf.

>  return(0);
> }
>
> and output is value 0 0

If you want to do this then a legal way is as follows:
#include <stdio.h>

int main()
{
int aa=0,b=0;
*(1>0?&aa:&b) = 10;
printf("value %d %d\n",aa,b);
return(0);
}
--
Flash Gordon

Report this thread to moderator Post Follow-up to this message
Old Post
Flash Gordon
03-31-08 12:11 AM


Re: ?: as an lvalue
Rahul wrote:
> Hi Everyone,
>
>  I have the following piece of code, and i expected an error, however
> i don't get an error,
>
> int main()
> {
> 	int aa=0,b=0;
> 	1>0?aa:b = 10;
>         printf("value %d %d\n",aa,b);
>  return(0);
> }
>
> and output is value 0 0
>
> Thanks in advance ! ! !

The result of the ?: operator is not an lvalue, and cannot be assigned to.

If you want to conditionally assign to one of two objects, it's probably
clearer to write it in full:

if (1>0)
aa = 0;
else
b = 10;

If you really want unreadable code, I suppose you could do this:
int *ip[2] = {&b,&aa};

*ip[1>0] = 10;

but if I worked with you I wouldn't thank you for it :)

Philip

Report this thread to moderator Post Follow-up to this message
Old Post
Philip Potter
03-31-08 12:11 AM


Re: ?: as an lvalue
"Philip Potter" <pgp@doc.ic.ac.uk> wrote in message
news:fso6hu$p5v$1@aioe.org...
> Rahul wrote:
 

> The result of the ?: operator is not an lvalue, and cannot be assigned to.
>
> If you want to conditionally assign to one of two objects, it's probably
> clearer to write it in full:
>
> if (1>0)
>    aa = 0;
> else
>    b = 10;
>

I think both these right-hand-sides should be 10.

Which is the problem, repeating a possibly complex expression.

But 'Flash' I think came up with simple mod of the OP's code which works and
keeps the code compact.

--
Bart



Report this thread to moderator Post Follow-up to this message
Old Post
Bartc
03-31-08 12:13 AM


Re: ?: as an lvalue
Rahul said:

> On Mar 30, 5:19 pm, Richard Heathfield <r...@see.sig.invalid> wrote: 
>
> I tried MS VC++ 8.0...

I think the switches you'll need for Visual C are -W4 -Za

(-W4 turns the warning level up as far as it'll go - unless they've added
another level in the last few years - and -Za switches off Microsoft
extensions.)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Report this thread to moderator Post Follow-up to this message
Old Post
Richard Heathfield
03-31-08 12:16 AM


Re: ?: as an lvalue
Bartc wrote:
> "Philip Potter" <pgp@doc.ic.ac.uk> wrote in message
> news:fso6hu$p5v$1@aioe.org... 
> 
> 
>
> I think both these right-hand-sides should be 10.
>
> Which is the problem, repeating a possibly complex expression.
>
> But 'Flash' I think came up with simple mod of the OP's code which works a
nd
> keeps the code compact.

Yes, I made a typo, but I'd still write it my way.

If you're worried about the complex expression being duplicated, use a
temporary:

int aa,b;
int tmp;
tmp = mycomplexexpr();
if (condition())
aa = tmp;
else
b = tmp;


Report this thread to moderator Post Follow-up to this message
Old Post
Philip Potter
03-31-08 12:16 AM


Sponsored Links




Last Thread Next Thread Next
Pages (10): [1] 2 3 4 5 6 » ... Last »
Search this forum -> 
Post New Thread

C archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 10:47 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.