Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Can we use break statement in conditional operator? (http://www.velocityreviews.com/forums/t548047-can-we-use-break-statement-in-conditional-operator.html)

jayapal 10-30-2007 11:47 AM

Can we use break statement in conditional operator?
 
Hi

the scenario is like this:

func()
{
....
....

char *p= (char *)&str[0];
while (p)
*++p=='$' ? break:(*p != ' ' ? return error : )
.....
.....

}

can i use conditional operator like these


James Kuyper 10-30-2007 11:51 AM

Re: Can we use break statement in conditional operator?
 
jayapal wrote:
> Hi
>
> the scenario is like this:
>
> func()
> {
> ...
> ...
>
> char *p= (char *)&str[0];
> while (p)
> *++p=='$' ? break:(*p != ' ' ? return error : )


No. the second operand in a conditional-expression is required to be an
expression. 'break;' and 'return error;' are statements, not an expressions.

vipvipvipvipvip.ru@gmail.com 10-30-2007 12:11 PM

Re: Can we use break statement in conditional operator?
 
Notice if str[] = "$" your func() will.. 'fail'.
Also, you want while(*p) and not while(p).
Also, if str is of type char * or char[], the cast is not needed.
&str[0] is a char *.

int func(const char * str) {

while(!isspace(*str))
if(*str == '$') break;

return *str == '$';
}


vipvipvipvipvip.ru@gmail.com 10-30-2007 12:12 PM

Re: Can we use break statement in conditional operator?
 
On Oct 30, 2:11 pm, vipvipvipvipvip...@gmail.com wrote:
> while(!isspace(*str))

sorry, while(*str && !isspace(*str))


vipvipvipvipvip.ru@gmail.com 10-30-2007 12:14 PM

Re: Can we use break statement in conditional operator?
 
On Oct 30, 2:12 pm, vipvipvipvipvip...@gmail.com wrote:
> On Oct 30, 2:11 pm, vipvipvipvipvip...@gmail.com wrote:> while(!isspace(*str))
>
> sorry, while(*str && !isspace(*str))


Damn, i don't increment str in the loop.
Sorry for this; it's been a long day


Chris Dollin 10-30-2007 12:21 PM

Re: Can we use break statement in conditional operator?
 
jayapal wrote:

> Hi
>
> the scenario is like this:
>
> func()
> {
> ...
> ...
>
> char *p= (char *)&str[0];
> while (p)
> *++p=='$' ? break:(*p != ' ' ? return error : )
> ....
> ....
>
> }
>
> can i use conditional operator like these


As at least one other person has said, no. And even if you /could/,
you couldn't write what you wrote, because there's nothing after
the second `:`. And even if you could do /that/, what exactly is
this loop supposed to accomplish? It /looks/ like (fx:emulate) it
skips the first character [if any] and then skips spaces and
either returns the error value if it's not at $-or-eos, or
falls through pointing at $-or-eos.

If so, I would have thought:

char *p = str; /* suspect the casting and indexing useless */

if (*p) p += 1;
while (*p == ' ') p += 1;

if (*p == '$') { whatever } else return error;

was more transparent. (I have a stylistic preference away from using
++ when the result isn't consumed; feel free to replace `p += 1` by
`p++` in the above if you have the reverse preference.)

--
Chris "would generally /prefer/ that `break` etc were expressions" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN


Chris Dollin 10-30-2007 12:31 PM

Re: Can we use break statement in conditional operator?
 
vipvipvipvipvip.ru@gmail.com wrote:

> Notice if str[] = "$" your func() will.. 'fail'.
> Also, you want while(*p) and not while(p).
> Also, if str is of type char * or char[], the cast is not needed.
> &str[0] is a char *.
>
> int func(const char * str) {
>
> while(!isspace(*str))
> if(*str == '$') break;
>
> return *str == '$';


We don't know that `func` returns `*str == '$'`, since the OP didn't
provide the remainder of its body after the loop.

> }


--
Chris "some documentation would have been helpful" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England



All times are GMT. The time now is 12:10 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57