Thread: comparing strings/arrays

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    2

    comparing strings/arrays

    Hello,

    I normally program embedded devices in ASM and my boss asked me to fix some code that was written in C, I have done a little C but not to much.

    The problem I've noticed is that a comparison doesn't always return true.

    We have an array of characters UBconfigData.code2[16] and we have a constant

    Code:
    #define VERSION			"2.10"
    #define ScaleType			"47 "
    #define ScaleVer			ScaleType VERSION
    when these two get compared sometimes true is return and sometimes it is not.

    Code:
    strcmp(UBconfigData.code2, ScaleVer )
    UBconfigData.code2 is set by this method

    Code:
    strcpy(configData.code2,ScaleVer); //Update code1,2 to current version
    I'm wondering does strcmp stop at the termination or does it continue to the end of the array?
    Should I do a memcmp and memcpy?

    Ron

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    strcmp does stop at the terminating \0 character at the end of a string. The thing to note is that "47 " "2.10" is one string (i.e., it is the same as "47 2.10"), so it's not as though you're getting cut off halfway. Is it possible that the strcmp is getting called before the strcpy? (Also, notice that if the two strings are the same, strcmp will return 0.)

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Just to clarify: you do understand that strcmp() does not return a boolean value, but rather returns a negative, zero, or positive value when the first argument is less than, equal to, or greater than the second argument respectively, right?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    2
    Yes I know this but I only care if they match or not.

    Code:
    if (strcmp(UBconfigData.code2, ScaleVer ) !=0) 
    {
         version changed
    }
    else
    {
        no change
    }

    with it being intermittent I figure I'm doing the check with the wrong function.

    Ron

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comparing data in two files
    By nynicue in forum C Programming
    Replies: 25
    Last Post: 06-18-2009, 07:35 PM
  2. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  3. bit vs bytes when comparing a file
    By Overworked_PhD in forum C Programming
    Replies: 6
    Last Post: 05-19-2007, 11:22 PM
  4. comparing problem
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 03-05-2002, 06:19 AM
  5. comparing struct members
    By breed in forum C Programming
    Replies: 4
    Last Post: 11-22-2001, 12:27 PM