comparing strings/arrays

This is a discussion on comparing strings/arrays within the C Programming forums, part of the General Programming Boards category; Hello, I normally program embedded devices in ASM and my boss asked me to fix some code that was written ...

  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,185
    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
    20,980
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 05:19 AM
  5. comparing struct members
    By breed in forum C Programming
    Replies: 4
    Last Post: 11-22-2001, 11:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21