Thread: strcmp()

  1. #1
    Registered User
    Join Date
    Oct 2012
    Location
    Portugal
    Posts
    20

    strcmp()

    I'm having trouble with strcmp(). I've read a topic on it, but couldn't understand.


    I'm using strcmp to compare a variable (that comes from fgets-sscanf pair) with the string "DATA" and others.

    I declared the variable as char, and already tried to put a limit on the number of characters of the variable both on strcmp and on the declaration of the variable, but it still doesn't work.
    Could you give me some help?

    Here is the code segment:

    Code:
    void leitura(char linha[200], info informacao){
    
    
        char verifica;
    
    
        /*Usa-se if e [strcmp] para verificar sscanfs*/
    
    
        sscanf(linha,"%s",&verifica);
    
    
        if(strcmp(verifica[4],"DATA")==0){
    
    
            sscanf(linha,"DATA %d %d %d\n",&informacao.dia,&informacao.mes,&informacao.ano);
            informacao.aviso=1;
        
                    }
    
    
        if(strcmp(verifica[4],"AREA")==0){
    
    
            sscanf(linha,"AREA %d %d %d %d\n",&informacao.x1,&informacao.y1,&informacao.x2,&informacao.y2);
            informacao.aviso=2;
                    }
    
    
        if(strcmp(verifica[9],"INDIVIDUO")==0){
    
    
            sscanf(linha,"INDIVIDUO %d %d %d %d\n",&informacao.especie,&informacao.individuo,&informacao.x_individuo,&informacao.y_individuo);
            informacao.aviso=3;
                        }
    
    
        if(strcmp(verifica[7],"COLONIA")==0){
    
    
            sscanf(linha,"COLONIA %d %d %d %d %d\n",&informacao.especie_col,&informacao.colonia,&informacao.tamanho,&informacao.x_colonia,&informacao.y_colonia);
            informacao.aviso=4;
                        }
    
    
        
        }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Code:
     char verifica;
    this is a char ,not a string.
    Hope this helps

  3. #3
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    verifica it's a character, not an array of characters.
    Try char verifica[200]; and check again arguments of strcmp.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Location
    Portugal
    Posts
    20
    I had already done that and the Warning is passing argument 1 of ‘strcmp’ makes pointer from integer without a cast

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Can you post the updated try?

  6. #6
    Registered User
    Join Date
    Oct 2012
    Location
    Portugal
    Posts
    20
    Here it is, this is a function, if you want I can post my main too.

    Code:
    void leitura(char linha[200], info informacao){
    
    
    	char verifica[200];
    
    
    	/*Usa-se if e [strcmp] para verificar sscanfs*/
    
    
    	sscanf(linha,"%s",&verifica);
    
    
    	if(strcmp(verifica[4],"DATA")==0){
    
    
    		sscanf(linha,"DATA %d %d %d\n",&informacao.dia,&informacao.mes,&informacao.ano);
    		informacao.aviso=1;
    	
    				}
    
    
    	if(strcmp(verifica[4],"AREA")==0){
    
    
    		sscanf(linha,"AREA %d %d %d %d\n",&informacao.x1,&informacao.y1,&informacao.x2,&informacao.y2);
    		informacao.aviso=2;
    				}
    
    
    	if(strcmp(verifica[9],"INDIVIDUO")==0){
    
    
    		sscanf(linha,"INDIVIDUO %d %d %d %d\n",&informacao.especie,&informacao.individuo,&informacao.x_individuo,&informacao.y_individuo);
    		informacao.aviso=3;
    					}
    
    
    	if(strcmp(verifica[7],"COLONIA")==0){
    
    
    		sscanf(linha,"COLONIA %d %d %d %d %d\n",&informacao.especie_col,&informacao.colonia,&informacao.tamanho,&informacao.x_colonia,&informacao.y_colonia);
    		informacao.aviso=4;
    					}
    
    
    	
    	}
    Thanks for the help!

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    This compiles??
    You have as argument char linha[200]
    When i need to pass something like that i use to write
    Code:
    void leitura(char* linha, int linhaSize, info informacao){..
    when i call the function i will pass 200 as 2nd parameter.Then inside the body of the function,you are able to treat linha as an array

  8. #8
    Registered User
    Join Date
    Oct 2012
    Location
    Portugal
    Posts
    20
    then inside what do I do so the function knows 200 is linha's size?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > sscanf(linha,"%s",&verifica);
    > if(strcmp(verifica[4],"DATA")==0)
    It's just

    sscanf(linha,"%s",verifica);
    if(strcmp(verifica,"DATA")==0)


    You don't say [4] because you're comparing 4 letters.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    This is for your convenience.If you do not need the size,do not pass it as a parameter

  11. #11
    Registered User
    Join Date
    Oct 2012
    Location
    Portugal
    Posts
    20
    Oh,so I'm not obliged to pass the size of linha into my new function?
    Okay,

    Thanks a lot, I'll see if it works and I'll update here

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by std10093 View Post
    This compiles??
    You have as argument char linha[200]
    When i need to pass something like that i use to write
    Code:
    void leitura(char* linha, int linhaSize, info informacao){..
    when i call the function i will pass 200 as 2nd parameter.Then inside the body of the function,you are able to treat linha as an array
    Yeah, it compiles. Arrays decay to a pointer to the first element when passed to a function, so the compile ignores any number in the left-most set of array brackets. If there's only one set of brackets, it ignores the number in there. It's not wrong to put it, but it's pointless. The following are all equivalent:
    Code:
    void leitura(char linha[200])  // the 200 is ignored, which is like having empty brackets, and...
    void leitura(char linha[])     // the empty brackets decay to a pointer
    void leitura(char *linha)    // so it ends up equivalent to this
    Note, this is only true for the left-most set of brackets. If you have a multi-dimensional array, you must specify all the dimensions except the left-most.
    Quote Originally Posted by Chinchila View Post
    then inside what do I do so the function knows 200 is linha's size?
    You pass a second parameter, like linhaSize, as std10093 suggested. Wherever you call the function, you must pass the correct size of the array into that parameter.

  13. #13
    Registered User
    Join Date
    Oct 2012
    Location
    Portugal
    Posts
    20
    Well, now strcmp is saying I'm making a pointer from an integer:

    Code:
    void leitura(char *linha, info informacao){
    
    
    	char verifica;
    
    
    	/*Usa-se if e [strcmp] para verificar sscanfs*/
    
    
    	sscanf(linha,"%s",&verifica);
    
    
    	if(strcmp(verifica,"DATA")==0){
    
    
    		sscanf(linha,"DATA %d %d %d\n",&informacao.dia,&informacao.mes,&informacao.ano);
    		informacao.aviso=1;
    	
    				}
    
    
    	if(strcmp(verifica,"AREA")==0){
    
    
    		sscanf(linha,"AREA %d %d %d %d\n",&informacao.x1,&informacao.y1,&informacao.x2,&informacao.y2);
    		informacao.aviso=2;
    				}
    
    
    	if(strcmp(verifica,"INDIVIDUO")==0){
    
    
    		sscanf(linha,"INDIVIDUO %d %d %d %d\n",&informacao.especie,&informacao.individuo,&informacao.x_individuo,&informacao.y_individuo);
    		informacao.aviso=3;
    					}
    
    
    	if(strcmp(verifica,"COLONIA")==0){
    
    
    		sscanf(linha,"COLONIA %d %d %d %d %d\n",&informacao.especie_col,&informacao.colonia,&informacao.tamanho,&informacao.x_colonia,&informacao.y_colonia);
    		informacao.aviso=4;
    					}
    
    
    	
    	}

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Because verifica, being a char, is an integer, but strcmp's first parameter is a pointer to char. The problem is that verifica should be an array, thus it will be converted to a pointer to char. Since it is only a char, your use of sscanf with the %s format specifier to read into it is wrong too.
    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

  15. #15
    Registered User
    Join Date
    Oct 2012
    Location
    Portugal
    Posts
    20
    So, what should I do to make it right?
    The logic is, I take from fgets(linha) into verifica with a sscanf, and then I want it to be compared in strcmp.
    I'm in the dark. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp
    By bob1223 in forum C Programming
    Replies: 4
    Last Post: 08-29-2010, 08:24 AM
  2. C++ Map vs. strcmp
    By Scarvenger in forum C++ Programming
    Replies: 16
    Last Post: 09-15-2007, 11:24 AM
  3. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM
  4. strcmp
    By joed in forum C Programming
    Replies: 4
    Last Post: 03-14-2005, 12:35 PM
  5. strcmp
    By Rob J in forum C++ Programming
    Replies: 1
    Last Post: 11-03-2004, 11:44 PM