May I ask why this works:
given:
char s[];
char *posbfr = s;
char *endbfr = s + MAXOP;
void(...){
if (posbfr >= endbfr)
printf("......");
else
*posbfr++ = c ;
}
but this is invalid?
(posbfr >= endbfr) ? printf("......") : *posbfr++ = c ;
(with an "invalid lvalue in assignment") error?
or...is this the dumbest question yet? :-)
Thank you.
|
|
0
|
|
|
|
Reply
|
mdeh (568)
|
10/28/2007 10:47:01 PM |
|
mdh wrote:
> May I ask why this works:
>
> given:
> char s[];
> char *posbfr = s;
> char *endbfr = s + MAXOP;
>
> void(...){
> if (posbfr >= endbfr)
> printf("......");
> else
> *posbfr++ = c ;
> }
>
>
> but this is invalid?
>
> (posbfr >= endbfr) ? printf("......") : *posbfr++ = c ;
>
You are assigning c to the result of the conditional expression, so the
above equivalent to
((posbfr >= endbfr) ? printf("......") : *posbfr++) = c ;
--
Ian Collins.
|
|
0
|
|
|
|
Reply
|
ian-news (9874)
|
10/28/2007 11:02:10 PM
|
|
mdh wrote:
> May I ask why this works:
<snip>
> but this is invalid?
>
> (posbfr >= endbfr) ? printf("......") : *posbfr++ = c ;
>
> (with an "invalid lvalue in assignment") error?
Assignment is lower priority than ?:. The compiler sees your expression
as (cond ? x : y) = c;
Try explicit parentheses, like this:
(posbfr >= endbfr) ? printf("......") : (*posbfr++ = c) ;
|
|
0
|
|
|
|
Reply
|
usenet3634 (45)
|
10/28/2007 11:03:09 PM
|
|
On Oct 28, 4:02 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> mdh wrote:
>
> > but this is invalid?
>
> > (posbfr >= endbfr) ? printf("......") : *posbfr++ = c ;
>
> Ian Collins wrote:
> You are assigning c to the result of the conditional expression, so the
> above equivalent to
>
> ((posbfr >= endbfr) ? printf("......") : *posbfr++) = c ;
>
Ian,
I know I am not then understanding this. I thought, given
expr1 ? expr2 : expr3;
then, if expr1 evaluates to true, then expr2 is evaluated, else if
false, expr3.
So, in the above case, is an assignment not regarded as a valid
expression?
..
|
|
0
|
|
|
|
Reply
|
mdeh (568)
|
10/28/2007 11:10:18 PM
|
|
On Oct 28, 4:03 pm, Peter Pichler <use...@pichler.co.uk> wrote:
> mdh wrote:
> > May I ask why this works:
>
>
> Try explicit parentheses, like this:
>
> (posbfr >= endbfr) ? printf("......") : (*posbfr++ = c) ;
Aha...thank you.
|
|
0
|
|
|
|
Reply
|
mdeh (568)
|
10/28/2007 11:11:20 PM
|
|