please help with strcmp()

Discussion in 'C Programming' started by Andrej Hocevar, Jul 19, 2003.

  1. Hello,
    I'm having problems with string comparisons.
    I have data like char d1[3] = {1, 2, 3} and char d2[1][3] = {1, 2,
    3}. Now if I do strcmp(d1, d2[1]), it compares only the first
    character, it seems. What is the correct solution?

    Thanks,
    andrej

    --
    echo ${girl_name} > /etc/dumpdates
     
    Andrej Hocevar, Jul 19, 2003
    #1
    1. Advertising

  2. In 'comp.lang.c', Andrej Hocevar <> wrote:

    > I'm having problems with string comparisons.
    > I have data like char d1[3] = {1, 2, 3} and char d2[1][3] = {1, 2,


    How is {1,2,3} a string?

    Remainder : "a string is an array of characters terminated by a 0".

    You want (maybe)

    char d1[] = {1, 2, 3, 0};

    or more likely

    char d1[] = {'1', '2', '3', 0};

    or

    char d1[] = "123";

    > 3}. Now if I do strcmp(d1, d2[1]), it compares only the first
    > character, it seems. What is the correct solution?


    You must be sure that the adresses passed to strcmp() points to valid
    strings.

    --
    -ed- [remove YOURBRA before answering me]
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    <blank line>
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
     
    Emmanuel Delahaye, Jul 19, 2003
    #2
    1. Advertising

  3. "Andrej Hocevar" <> wrote in message
    news:...
    >
    > I'm having problems with string comparisons.
    > I have data like char d1[3] = {1, 2, 3} and char d2[1][3] = {1, 2,
    > 3}. Now if I do strcmp(d1, d2[1]), it compares only the first
    > character, it seems. What is the correct solution?


    A string is a sequence of characters that ends with a null character. I
    don't see any strings in your example, and I can't figure out what you mean
    to do. Maybe you should be using strncmp or memcmp? In any case, the
    second argument to strcmp--d2[1]--produces undefined results. There is no
    d2[1]. There is only d2[0].

    --
    Russell Hanneken
     
    Russell Hanneken, Jul 19, 2003
    #3
  4. >I'm having problems with string comparisons.
    >I have data like char d1[3] = {1, 2, 3} and char d2[1][3] = {1, 2,
    >3}. Now if I do strcmp(d1, d2[1]), it compares only the first
    >character, it seems.


    How did you determine this? You obviously used more code than
    described above.

    >What is the correct solution?


    A string has a '\0' terminator. The crap you are passing to
    strcmp() above doesn't, so it's not a string.

    Gordon L. Burditt
     
    Gordon Burditt, Jul 19, 2003
    #4
    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,359
    Kevin Goodsell
    Sep 22, 2003
  2. muser

    strcmp

    muser, Oct 3, 2003, in forum: C++
    Replies:
    6
    Views:
    1,122
    Frank Schmitt
    Oct 9, 2003
  3. KK
    Replies:
    2
    Views:
    601
    Big Brian
    Oct 14, 2003
  4. Allan Bruce

    strcmp but with '\n' as the terrminator

    Allan Bruce, Jul 19, 2003, in forum: C Programming
    Replies:
    53
    Views:
    1,393
    Arthur J. O'Dwyer
    Jul 30, 2003
  5. Marcus
    Replies:
    14
    Views:
    670
    Eberhard Schefold
    Jun 30, 2008
Loading...

Share This Page