Thread: strcmp: which is which?

  1. #1
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266

    strcmp: which is which?

    hey all, hope you had a fine weekend does someone mind explaining which parameter is which in this. i know it compares two strings, but i can't tell which does what? thanks

    Code:
    if(strcmp(n[index],n[index+1])>0)

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    It looks like this is an array of char *, strcmp tests one string in the array with the next string in the array. n[index] is the first argument, n[index+1] is the second.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    I believe strcmp returns 0 if the 2 strings are of equal value. And I suppose those would be arrays of char*, because strcmp doesn't accept chars as parameters...

  4. #4
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    sorry should have posted more. what's the >0 at the end. is this the right track: if index+1 is greater than index than . . .

    Code:
    for(index=0;index<max-1;index++)
    {
                   if(strcmp(n[index],n[index+1])>0)
                   {
    	strcpy(temp,n[index]);
    	strcpy(n[index],n[index+1]);
    	strcpy(n[index+1],temp);
    	swapflag=1;
    	}

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    If strcmp returns 0 then the strings are equal, if it returns greater or less than 0 then the strings aren't equal.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    strcmp(a, b) > 0 if a > b
    strcmp(a, b) == 0 if a == b
    strcmp(a, b) < 0 if a < b

    AFAIK anyway.

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