strcmp, please help me understand this

Discussion in 'C++' started by Marcus, Jun 27, 2008.

  1. Marcus

    Marcus Guest

    hi, i'm really confused as to what's going on here:

    char *foo = "ABC";
    char *foo2 = "ABC;

    if(strcmp(foo,foo2) == 0)
    print("Works");

    it doesn't print, when i step into strcmp the values in strcmp.asm are
    always str1 "ABC", str2 "BÍÍÍ". it doesn't matter the order str2 is
    always junk.

    how can this be? am i not understanding how strcmp works?

    thanks
     
    Marcus, Jun 27, 2008
    #1
    1. Advertising

  2. Marcus

    Marcus Guest

    char *foo = "ABC";
    char *foo2 = "ABC";

    if(strcmp(foo,foo2) == 0)
    printf("Works");

    i missed a quote in foo2 and meant printf. sorry it became completely
    meaningless with those errors.

    the basic point is that char*'s aren't strcmp'ing and i'm not sure
    why. my code compiles.

    > The code you have shown should not compile.
    >
    > Show the real code.
    >
    > Remember: first convert tabs to spaces (if you have tabs), then copy and paste
    > the code into your article  --  don't retype the code.
    >
    > Cheers, & hth.,
    >
    > - Alf
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?
     
    Marcus, Jun 27, 2008
    #2
    1. Advertising

  3. Marcus

    Joe Greer Guest

    Marcus <> wrote in news:c8492b59-57c9-4875-832d-
    :

    > hi, i'm really confused as to what's going on here:
    >
    > char *foo = "ABC";
    > char *foo2 = "ABC;
    >
    > if(strcmp(foo,foo2) == 0)
    > print("Works");
    >
    > it doesn't print, when i step into strcmp the values in strcmp.asm are
    > always str1 "ABC", str2 "BÍÍÍ". it doesn't matter the order str2 is
    > always junk.
    >
    > how can this be? am i not understanding how strcmp works?
    >
    > thanks
    >


    After fixing the errors, your code above works fine for me. So, what is
    the real code?

    joe
     
    Joe Greer, Jun 27, 2008
    #3
  4. Marcus

    Marcus Guest

    you guys are funny. i thought this was comp.lang.c++... not
    comp.we.can.only.comment.on things.that.can.be.compiled.c++

    back in the day in this group, the jibe replies would've looked like:
    "char*'s WILL strcmp, assuming you didn't mean those errors above,
    your error is somewhere else."

    times change i guess. thanks though joe, appreciate you taking the
    time to actually compile that. the error must be somewhere else.
     
    Marcus, Jun 27, 2008
    #4
  5. Marcus

    Joe Greer Guest

    Marcus <> wrote in news:b4aea5d2-b49b-43a7-ab39-
    :

    > you guys are funny. i thought this was comp.lang.c++... not
    > comp.we.can.only.comment.on things.that.can.be.compiled.c++
    >
    > back in the day in this group, the jibe replies would've looked like:
    > "char*'s WILL strcmp, assuming you didn't mean those errors above,
    > your error is somewhere else."
    >
    > times change i guess. thanks though joe, appreciate you taking the
    > time to actually compile that. the error must be somewhere else.


    The point is that strcmp works as advertised, so there is something else
    going on corrupting things. Since you are messing with pointers, I would
    look for boundary overwrites or writing to invalid pointers. A tool like
    Purify can be valuable for finding these sorts of things.

    joe
     
    Joe Greer, Jun 27, 2008
    #5
  6. Marcus wrote:
    > you guys are funny. i thought this was comp.lang.c++... not
    > comp.we.can.only.comment.on things.that.can.be.compiled.c++
    >
    > back in the day in this group, the jibe replies would've looked like:
    > "char*'s WILL strcmp, assuming you didn't mean those errors above,
    > your error is somewhere else."


    What's wrong with that answer? It looks perfectly accurate to me.

    Fixing your typing mistakes and putting a minimal complete program
    around it makes it work as it should. Thus your lines don't show any
    error and thus it's impossible to answer your question.
     
    Juha Nieminen, Jun 29, 2008
    #6
  7. Marcus

    CD1 Guest

    > #include <string>
    > #include <iostream>
    >
    > int main()
    > {
    >     std::string foo("ABC");
    >     std::string f002("ABC");
    >
    >     if(foo == foo2)
    >         std::cout << "match\n";
    >     else
    >         std::cout << "no match\n";
    >
    >     return 0;
    >
    > }


    This code won't compile. There is no variable called "foo2".

    :)
     
    CD1, Jun 30, 2008
    #7
  8. Marcus

    Marcus Guest

    On Jun 29, 8:58 pm, CD1 <> wrote:
    > > #include <string>
    > > #include <iostream>

    >
    > > int main()
    > > {
    > >     std::string foo("ABC");
    > >     std::string f002("ABC");

    >
    > >     if(foo == foo2)
    > >         std::cout << "match\n";
    > >     else
    > >         std::cout << "no match\n";

    >
    > >     return 0;

    >
    > > }

    >
    > This code won't compile. There is no variable called "foo2".
    >
    > :)- Hide quoted text -
    >
    > - Show quoted text -


    haha.... i wasn't going to say anything, but that made me laugh... yea
    mike, i simply can't understand what you're trying to say here at
    all! ;)

    thanks for the help guys, seriously. i'm just trying to point out,
    that communication is a lot easier when the listener tries to
    understand the meaning behind things, rather than dismissing the
    entirety if an i wasn't dotted or a t wasn't crossed. i think yall get
    what i'm saying... or did i miss an apostophe somewhere?

    regards,
    marcus
     
    Marcus, Jun 30, 2008
    #8
  9. Hi!

    Marcus schrieb:
    > hi, i'm really confused as to what's going on here:
    >
    > char *foo = "ABC";
    > char *foo2 = "ABC;


    Change these to:

    const char* const foo = "ABC";
    const char* const foo2 = "ABC";

    And try again. If this does not discover your problem then you are in
    real trouble.

    > how can this be? am i not understanding how strcmp works?


    It should work as expected but you seem to mess with the data somewhere.
    Somewhere we can't see.

    Frank
     
    Frank Birbacher, Jun 30, 2008
    #9
  10. Marcus

    Lionel B Guest

    On Sun, 29 Jun 2008 22:04:31 -0700, Marcus wrote:

    > On Jun 29, 8:58 pm, CD1 <> wrote:
    >> > #include <string>
    >> > #include <iostream>

    >>
    >> > int main()
    >> > {
    >> >     std::string foo("ABC");
    >> >     std::string f002("ABC");

    >>
    >> >     if(foo == foo2)
    >> >         std::cout << "match\n";
    >> >     else
    >> >         std::cout << "no match\n";

    >>
    >> >     return 0;

    >>
    >> > }

    >>
    >> This code won't compile. There is no variable called "foo2".
    >>
    >> :)- Hide quoted text -
    >>
    >> - Show quoted text -

    >
    > haha.... i wasn't going to say anything, but that made me laugh... yea
    > mike, i simply can't understand what you're trying to say here at all!
    > ;)


    He was making the reasonable and potentially helpful suggestion that you
    might consider using the the std::string class rather than char*, as it
    makes comparison much simpler and less error-prone.

    > thanks for the help guys, seriously. i'm just trying to point out, that
    > communication is a lot easier when the listener tries to understand the
    > meaning behind things, rather than dismissing the entirety if an i
    > wasn't dotted or a t wasn't crossed. i think yall get what i'm saying...
    > or did i miss an apostophe somewhere?


    Seeing as you're asking a bunch of total strangers to take the time and
    trouble to help you, don't you think it might be a good idea to make it
    easier for them to do so? This is why people request complete, compilable
    code: it makes it easier to help.

    --
    Lionel B
     
    Lionel B, Jun 30, 2008
    #10
  11. Marcus wrote:

    > thanks for the help guys, seriously. i'm just trying to point out,
    > that communication is a lot easier when the listener tries to
    > understand the meaning behind things, rather than dismissing the
    > entirety if an i wasn't dotted or a t wasn't crossed. i think yall get
    > what i'm saying... or did i miss an apostophe somewhere?


    These guys were trying to help you, and they can't if you don't tell
    them what the problem is. The misunderstanding is on your own part if
    you regard this as nitpicking. They were asking what the problem is,
    since your initial code obviously was not apt to demonstrate the problem.

    If a stranger came to you on the street and asked you "Where's Zelig
    Street?", and you're answering "There is no Zelig Street here, are you
    sure you got the right name?", and he started complaining over your
    manners, that you're not listening, etc., what would you think?
     
    Eberhard Schefold, Jun 30, 2008
    #11
  12. Marcus

    Marcus Guest

    On Jun 30, 3:38 am, Eberhard Schefold <>
    wrote:
    > Marcus wrote:
    > > thanks for the help guys, seriously. i'm just trying to point out,
    > > that communication is a lot easier when the listener tries to
    > > understand the meaning behind things, rather than dismissing the
    > > entirety if an i wasn't dotted or a t wasn't crossed. i think yall get
    > > what i'm saying... or did i miss an apostophe somewhere?

    >
    > These guys were trying to help you, and they can't if you don't tell
    > them what the problem is. The misunderstanding is on your own part if
    > you regard this as nitpicking. They were asking what the problem is,
    > since your initial code obviously was not apt to demonstrate the problem.
    >
    > If a stranger came to you on the street and asked you "Where's Zelig
    > Street?", and you're answering "There is no Zelig Street here, are you
    > sure you got the right name?", and he started complaining over your
    > manners, that you're not listening, etc., what would you think?


    I was clear. I asked what I wasn't understanding about how strcmp
    works, specifically with char*'s. I gave an example using foo, but
    flubbed a quote and print instead of printf. No one until AFTER I
    quipped that those were nitpicky errors tried to actually answer my
    question. It IS nitpicking, just like it would be nitpicking for me to
    say that Mike makes no sense because he accidently used f002 instead
    of foo2... it's stupid for me to INTENTIONALLY not try and understand
    what he's saying about using string's instead of char*'s. I looked
    past the error and understood what he was trying to say, this is how
    basic communication works with simple subjects. If you want to make
    things COMPLICATED and TENSE, then yea, pretending you don't
    understand is the right way to go about things.

    Your analogy isn't what happend, more like, the stranger asked where
    zelig street was with a thick Mexican accent, and I answered, "learn
    how to speak english, i can't understand you"... even though I know
    exactly what he was saying... it's just, well, any Mexican who can't
    pronunciate perfectly shouldn't be in this country, right?
     
    Marcus, Jun 30, 2008
    #12
  13. Marcus

    Jerry Coffin Guest

    In article <ad4e6803-e381-4a5d-b4d7-ed1554319df3
    @g16g2000pri.googlegroups.com>, says...

    [ ... ]

    > I was clear. I asked what I wasn't understanding about how strcmp
    > works, specifically with char*'s. I gave an example using foo, but
    > flubbed a quote and print instead of printf. No one until AFTER I
    > quipped that those were nitpicky errors tried to actually answer my
    > question. It IS nitpicking, just like it would be nitpicking for me to
    > say that Mike makes no sense because he accidently used f002 instead
    > of foo2...


    While it's (sort of) true that what they did was mostly nitpicking,
    doing so was a lot more reasonable than you seem to realize. In
    particular, even if it looked like nitpicking, it pointed out a much
    more fundamental problem with your post.

    Your question _should_ have been accompanied by complete, compilable
    code that demonstrates the problem. That, however, is only meaningful
    when/if we're sure that what we're looking at is _precisely_ the same
    code that you're having a problem with. The fact that you had print
    instead of printf shows that what you posted was NOT precisely the same
    as the code you were dealing with -- and it's impossible for us to be
    certain that something on the same order (e.g. another typo) wasn't the
    source of the real problem.

    As such, when you post a problem like this, it's important that you cut
    and paste or insert the text of the code directly from the file you
    tried to compile, or something on that order. Retyping the code into the
    article (which is quite apparently what you did) when you post it may
    easily fix the problem you had in the first place, and/or may easily
    leave out the part that really caused the problem you were experiencing
    (which is what seems to have happened here).

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Jun 30, 2008
    #13
  14. Marcus

    Lionel B Guest

    On Mon, 30 Jun 2008 07:51:51 -0700, Marcus wrote:

    > On Jun 30, 3:38 am, Eberhard Schefold <>
    > wrote:
    >> Marcus wrote:
    >> > thanks for the help guys, seriously. i'm just trying to point out,
    >> > that communication is a lot easier when the listener tries to
    >> > understand the meaning behind things, rather than dismissing the
    >> > entirety if an i wasn't dotted or a t wasn't crossed. i think yall
    >> > get what i'm saying... or did i miss an apostophe somewhere?

    >>
    >> These guys were trying to help you, and they can't if you don't tell
    >> them what the problem is. The misunderstanding is on your own part if
    >> you regard this as nitpicking. They were asking what the problem is,
    >> since your initial code obviously was not apt to demonstrate the
    >> problem.


    [...]

    > I was clear. I asked what I wasn't understanding about how strcmp works,
    > specifically with char*'s. I gave an example using foo, but flubbed a
    > quote and print instead of printf. No one until AFTER I quipped that
    > those were nitpicky errors tried to actually answer my question.


    As I see it, it turned out that -- modulo your nitpicky errors -- the
    problem *wasn't* in the code fragment you showed us. If you had made the
    basic effort to construct a complete, minimal, compilable, program as per
    the FAQ for this ng:

    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.8

    then you would have immediately spotted this for yourself and wouldn't
    have had to post here at all!

    --
    Lionel B
     
    Lionel B, Jun 30, 2008
    #14
  15. Marcus wrote:

    > I was clear. I asked what I wasn't understanding about how strcmp
    > works, specifically with char*'s. I gave an example using foo, but
    > flubbed a quote and print instead of printf. No one until AFTER I
    > quipped that those were nitpicky errors tried to actually answer my
    > question. It IS nitpicking, just like it would be nitpicking for me to
    > say that Mike makes no sense because he accidently used f002 instead
    > of foo2...


    That your code didn't compile was the clear indication that you were not
    even presenting the actual offending code snippet, let alone the
    compilable minimal example that is recommended in such cases. That's
    what people were trying to tell you. They can't help you find a solution
    if you don't give them the problem. There was (and still is) no way to
    extrapolate the problem from what you posted.

    > Your analogy isn't what happend,


    Right! It's more like "Tell me where Zigzag-whatever Street is. I have
    the exact name in my notebook but I can't be bothered to look it up and
    tell you. --- (People used to be a lot friendlier in this neighborhood.
    Nobody listens anymore.)" :p
     
    Eberhard Schefold, Jun 30, 2008
    #15
    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,362
    Kevin Goodsell
    Sep 22, 2003
  2. muser

    strcmp

    muser, Oct 3, 2003, in forum: C++
    Replies:
    6
    Views:
    1,124
    Frank Schmitt
    Oct 9, 2003
  3. KK
    Replies:
    2
    Views:
    624
    Big Brian
    Oct 14, 2003
  4. Andrej Hocevar

    please help with strcmp()

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

    strcmp but with '\n' as the terrminator

    Allan Bruce, Jul 19, 2003, in forum: C Programming
    Replies:
    53
    Views:
    1,396
    Arthur J. O'Dwyer
    Jul 30, 2003
Loading...

Share This Page