strcmp();

This is a discussion on strcmp(); within the C Programming forums, part of the General Programming Boards category; I want to use strcmp(); in my program. I have read that it needs two string constants. If I have ...

  1. #1
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    strcmp();

    I want to use strcmp(); in my program.
    I have read that it needs two string constants.
    If I have a string say string1[99], and string2[99]. I input data into it and add a trailling \0. Does adding the trailing \0 create the string constant? My book states it has to be a string enclosed in "string". Can we not use a string scaned in by sscanf? I want to compare the two strings with the following syntax.
    strcmp(string1,string2);.

    sample program... sorry no compiler to check
    Code:
    #include<string.h>
    #include<stdio>
    char string1[99];
    char string2[99];
    memset(string1,'\0',99);
    memset(string2,'\0',99);
    int x;
    scanf("%s%s",string1,string2);
    x=strcmp(string1,string2);
    printf("%i",x);
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Any series of char, terminated by a '\0', makes up a string. If it's in a non-const array, then it itself isn't a "constant", meaning you can change it. The arguments to strcmp are given the const attribute, meaning that strcmp will not modify them within the function call itself.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    So does that mean the above code is valid?
    I do not see any syntax issues, but I again do not have a compiler to show how sloppy it is written.
    So do I need to change the strings to const, or does the function do this already?
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    It is made const when the function gets it to ensure the function won't change it, you don't have to make it const yourself, the compiler does it when you pass the strings as args.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, you don't need to make the arrays constant. When something is declared as constant in a function definition or prototype, it means that that argument will not or cannot be changed inside that function.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    does that mean the code i wrote is valid?
    i remember having issues with this last time i wrote it in C++.
    and i currently do not have a compiler to test it.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well yes, or no. Depending on your compiler. It's C99 or C++ code, in that you're declaring variables after you make a function call, meaning, all of your variables aren't declared at the first of the code block.

    Yes, on a C99 compiler it would work. However, there's no need for the memset calls if you just delcare your arrays like so:
    Code:
    char array[somesize] = {0};
    Because that initializes the first element of the array to zero, and all the rest default to zero, because any element uninitialized once you've initialized some of them, default to zero.

    Anyway, yeah, it should be fine.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    I tried on a prior c++ assignment to initialize the array with
    Code:
    array[SIZE]={\0};
    but all other indexes other than array index 0 printed garbage.
    it could have been other aspects of the program affecting it though.
    doesn't the compiler read the code as initialize the array location referenced by SIZE to \0.

    But then again, doesn't scanf place a null at the end of the string?
    Would this be the same for sscanf?

    I guess this is how it should have been written.
    Code:
    #include<string.h>
    #include<stdio>
     
    int x;
     
    char string1[99]={\0};
    char string2[99]={\0};
     
    scanf("%s%s",string1,string2);
     
    x=strcmp(string1,string2);
     
    printf("%i",x);
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char string1[99]={\0};
    That's wrong. If you're trying to put the null in the first element, since it's a single character, you must enclose it in single quotes:
    Code:
    char string1[99]={'\0'};
    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    oops.
    I always forget something stupid in my syntax.
    I would have probably been searching for that error for a couple days.

    Code:
    #include<string.h>
    #include<stdio>
     
    int x;
     
    char string1[99]={'\0'};
    char string2[99]={'\0'};
    char string3[99]={'\0'};
     
    scanf("%s%s",string1,string2);
     
    x=strcmp(string1,string2);
     
    printf("%i",x);
    
    x=strcpy(string3,string1);
    
    printf("%i",x);
    I'm assuming since I have no compiler, but would this also be valid?
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  11. #11
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    I guess it would work, but you should use fgets() instead of scanf(), and there is no way to tell when one string ends and one begins, you should put some character between them so you can seperate them, like scanf("%s_%s", string1, string2);, or fgets() and sscanf().
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    and there is no way to tell when one string ends and one begins,
    Any white space constitutes a string end with %s.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    scanf("%s%s",string1,string2);
    Instead of this, you should use gets(). gets() is safer.

  14. #14
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Instead of this, you should use gets(). gets() is safer.
    Which isn't saying much.

  15. #15
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Quote Originally Posted by anonytmouse
    Code:
    scanf("%s%s",string1,string2);
    Instead of this, you should use gets(). gets() is safer.
    After over 500 posts you should know better than to suggest gets(). Read the FAQ.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Page 1 of 2 12 LastLast
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, 05: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, 07:23 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM

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