Thread: strcmp()

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    19

    strcmp()

    hi, im using strcmp to compare a character pointer char *cptr with a character array
    char text[100] using if(strcmp(cptr, text)==0) printf("%s", cptr);

    my debugger shows this....

    cptr = 0x0012F028 "orange" char*
    text "orange" char[]

    the result of strcmp(cptr, text)==0 will always be false... why?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Are both arrays null terminated?

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    show code where you initialize both strings
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    i used strtok to get 2 string whereby each string is seperated by SPACE. then i compare them using strcmp. is dat wat causes the error because using strtok removes the "\n" ? if so how do i actually compare the string?
    Last edited by norhos; 03-15-2008 at 01:32 AM.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by norhos View Post
    i used strtok to get 2 string whereby each string is seperated by SPACE. then i compare them using strcmp. is dat wat causes the error because using strtok removes the "\n" ? if so how do i actually compare the string?
    show the code instead of your interpretation what is going on...
    Small compileable samle representing the problem
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    check out the strcmp at the last while loop
    Last edited by norhos; 03-15-2008 at 07:55 AM.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    if(strcmp(currNode->words, ptr ))
    You're missing the comparison to 0.... the one that you told us you were doing.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    even if i put if(strcmp(currNode->words, ptr ) == 0) still return false when my input is shown below is being compared

    name, value, type

    currNode->words, 0x003a1d30 "orange", char *
    ptr, 0x0012fe78 "orange", char *
    Last edited by norhos; 03-15-2008 at 02:31 AM.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    and what about sample file you are reading?
    I suppose the problem is '\n' char you do not remove so one string contains it and another - does not...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    hmmm i dun understand. which part did i removed the \n? isit the strtok ?

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by norhos View Post
    hmmm i dun understand. which part did i removed the \n? isit the strtok ?
    if one string is
    "orange apple\n"
    and second
    "apple orange\n"

    with your strtok approach you finish with comparing

    "orange" to "orange\n"
    and "apple\n" to "apple"

    both comparisomns will fail
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    no wonder.. one of my text file is like and essay and the other is one word per line...
    so.. any advise on how to solve the problem or is there other way to compare the string without using strcmp?
    Last edited by norhos; 03-15-2008 at 04:40 AM.

  13. #13
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    yea i already fixed that. now its jus only the strcmp issue

  14. #14
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    http://cboard.cprogramming.com/showthread.php?t=100358
    Read the last post, I've already attempted to sort some of your issues out.

    You obviously haven't done what I've suggested.

  15. #15
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    Thanks for the advice everyone, i manage to solve it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 06:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 04:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  4. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 08:23 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM