Can we use break statement in conditional operator?

Discussion in 'C Programming' started by jayapal, Oct 30, 2007.

  1. jayapal

    jayapal Guest

    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
     
    jayapal, Oct 30, 2007
    #1
    1. Advertising

  2. jayapal

    James Kuyper Guest

    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.
     
    James Kuyper, Oct 30, 2007
    #2
    1. Advertising

  3. jayapal

    Guest

    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 == '$';
    }
     
    , Oct 30, 2007
    #3
  4. jayapal

    Guest

    On Oct 30, 2:11 pm, wrote:
    > while(!isspace(*str))

    sorry, while(*str && !isspace(*str))
     
    , Oct 30, 2007
    #4
  5. jayapal

    Guest

    On Oct 30, 2:12 pm, wrote:
    > On Oct 30, 2:11 pm, 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
     
    , Oct 30, 2007
    #5
  6. jayapal

    Chris Dollin Guest

    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, Oct 30, 2007
    #6
  7. jayapal

    Chris Dollin Guest

    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
     
    Chris Dollin, Oct 30, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    807
  2. Armin

    conditional break

    Armin, Jul 29, 2004, in forum: HTML
    Replies:
    2
    Views:
    2,518
    nice.guy.nige
    Jul 29, 2004
  3. Alec S.
    Replies:
    10
    Views:
    10,157
    Alec S.
    Apr 16, 2005
  4. Replies:
    1
    Views:
    15,408
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Mar 13, 2007
  5. Replies:
    12
    Views:
    968
Loading...

Share This Page