do I need strcmp ?

Discussion in 'C Programming' started by John Gordon, Nov 4, 2011.

  1. John Gordon

    John Gordon Guest

    In <4eb4605c$0$20319$> "Bill Cunningham" <> writes:

    > I am doing a comparison here and the code works and then doesn't work.
    > My goal is to start a string to be recognized by a function only if it
    > starts with a +. Kind of like cpp doesn't recognize preprocessor commands
    > unless # is present.


    > #include <stdio.h>
    > #include <string.h>


    > int main()
    > {
    > char *plus;
    > char *string = "+ hello there\n";
    > plus = strchr(string, '+');
    > if ((int) plus[0] == '+')
    > printf("ok\n");
    > if ((int) plus[0] != '+')
    > printf("no\n");
    > }


    > This code as it is works marvelously when finding + in a string. If
    > something other than + is used, I do not get no but a segmentation fault. Is


    That's because strchr returns NULL when the target character is not found,
    which causes 'plus' to have a null value. Then, when you attempt to
    look at plus[0], the program crashes.

    > the only way around this to use strcmp to see if the first character of the
    > string is + or not? Can the code be changed to accomplish what I want
    > without using strcmp?


    Using strchr seems like overkill in this case. You don't even need the
    'plus' variable. Just look for the character directly:

    char *string = "+hello there\n";

    if(string[0] == '+')
    printf("ok\n");
    else
    printf("no\n");

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Nov 4, 2011
    #1
    1. Advertising

  2. On 11/4/2011 6:59 PM, Bill Cunningham wrote:
    > I am doing a comparison here and the code works and then doesn't work.
    > My goal is to start a string to be recognized by a function only if it
    > starts with a +.


    If the string is in, say, an array named 'command',
    just check as follows:

    if (command[0] == '+') {
    /* do stuff */
    }
    else {
    /* don't do stuff */
    }
    Martin Ambuhl, Nov 4, 2011
    #2
    1. Advertising

  3. I am doing a comparison here and the code works and then doesn't work.
    My goal is to start a string to be recognized by a function only if it
    starts with a +. Kind of like cpp doesn't recognize preprocessor commands
    unless # is present.

    #include <stdio.h>
    #include <string.h>

    int main()
    {
    char *plus;
    char *string = "+ hello there\n";
    plus = strchr(string, '+');
    if ((int) plus[0] == '+')
    printf("ok\n");
    if ((int) plus[0] != '+')
    printf("no\n");
    }

    This code as it is works marvelously when finding + in a string. If
    something other than + is used, I do not get no but a segmentation fault. Is
    the only way around this to use strcmp to see if the first character of the
    string is + or not? Can the code be changed to accomplish what I want
    without using strcmp?

    Bill
    Bill Cunningham, Nov 4, 2011
    #3
  4. John Gordon wrote:
    > In <4eb4605c$0$20319$> "Bill Cunningham"
    > <> writes:
    >
    >> I am doing a comparison here and the code works and then doesn't
    >> work. My goal is to start a string to be recognized by a function
    >> only if it starts with a +. Kind of like cpp doesn't recognize
    >> preprocessor commands unless # is present.

    >
    >> #include <stdio.h>
    >> #include <string.h>

    >
    >> int main()
    >> {
    >> char *plus;
    >> char *string = "+ hello there\n";
    >> plus = strchr(string, '+');
    >> if ((int) plus[0] == '+')
    >> printf("ok\n");
    >> if ((int) plus[0] != '+')
    >> printf("no\n");
    >> }

    >
    >> This code as it is works marvelously when finding + in a string. If
    >> something other than + is used, I do not get no but a segmentation
    >> fault. Is

    >
    > That's because strchr returns NULL when the target character is not
    > found,
    > which causes 'plus' to have a null value. Then, when you attempt to
    > look at plus[0], the program crashes.
    >
    >> the only way around this to use strcmp to see if the first character
    >> of the string is + or not? Can the code be changed to accomplish
    >> what I want without using strcmp?

    >
    > Using strchr seems like overkill in this case. You don't even need
    > the 'plus' variable. Just look for the character directly:
    >
    > char *string = "+hello there\n";
    >
    > if(string[0] == '+')
    > printf("ok\n");
    > else
    > printf("no\n");


    Thanks that NULL completely slipped my mind.

    Bill
    Bill Cunningham, Nov 4, 2011
    #4
  5. On Nov 4, 10:36 pm, Martin Ambuhl <> wrote:
    > On 11/4/2011 6:59 PM, Bill Cunningham wrote:
    >
    > >      I am doing a comparison here and the code works and then doesn't work.
    > > My goal is to start a string to be recognized by a function only if it
    > > starts with a +.

    >
    > If the string is in, say, an array named 'command',
    > just check as follows:
    >
    >     if (command[0] == '+') {
    >        /* do stuff */
    >     }
    >     else {
    >        /* don't do stuff */
    >     }


    first ensuring that command contains at least one character
    Nick Keighley, Nov 6, 2011
    #5
  6. Nick Keighley <> writes:
    > On Nov 4, 10:36 pm, Martin Ambuhl <> wrote:
    >> On 11/4/2011 6:59 PM, Bill Cunningham wrote:
    >>
    >> >      I am doing a comparison here and the code works and then doesn't work.
    >> > My goal is to start a string to be recognized by a function only if it
    >> > starts with a +.

    >>
    >> If the string is in, say, an array named 'command',
    >> just check as follows:
    >>
    >>     if (command[0] == '+') {
    >>        /* do stuff */
    >>     }
    >>     else {
    >>        /* don't do stuff */
    >>     }

    >
    > first ensuring that command contains at least one character


    If command is an array object, then no such check is necessary.
    If it contains an empty string, command[0] is '\0'. If it's a
    zero-length array, then, well, the program must be in a language
    other than C that supports such things.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Nov 6, 2011
    #6
    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. Shane Peck

    strcmp problem

    Shane Peck, Sep 19, 2003, in forum: C++
    Replies:
    6
    Views:
    2,340
    Kevin Goodsell
    Sep 22, 2003
  2. muser

    strcmp

    muser, Oct 3, 2003, in forum: C++
    Replies:
    6
    Views:
    1,095
    Frank Schmitt
    Oct 9, 2003
  3. Andrej Hocevar

    please help with strcmp()

    Andrej Hocevar, Jul 19, 2003, in forum: C Programming
    Replies:
    3
    Views:
    320
    Gordon Burditt
    Jul 19, 2003
  4. Allan Bruce

    strcmp but with '\n' as the terrminator

    Allan Bruce, Jul 19, 2003, in forum: C Programming
    Replies:
    53
    Views:
    1,367
    Arthur J. O'Dwyer
    Jul 30, 2003
  5. Robert Mens

    segfault when strcmp

    Robert Mens, Oct 22, 2003, in forum: C Programming
    Replies:
    6
    Views:
    565
    Peter Nilsson
    Oct 23, 2003
Loading...

Share This Page