Thread: Simple strcmp parameter problem.....

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    93

    Exclamation Simple strcmp parameter problem.....

    With the following code I am recieving the error in the first parameter of strcmp

    Code:
    void strip(char original[], char stripped[]){
    
    	// Remove whitespace characters
    	
    	
    	for(int i = 0;i < original[i]; i++){
    	
    		if(strcmp(original[i],"a")==0){
    			printf("Found an a");
    		}
    	}
    }

    [code]passing arg1 makes pointer from integer without a cast[code]

    Main....

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void strip(char original[], char stripped[]);
    void reverse(char before[], char after[]);
    #define SIZE 100
    
    int main(void){
    
    	char original[SIZE];
    	char stripped[SIZE];
    	
    	printf("Please enter a string: ");
    		gets(original);
    	printf("\nYour string was: %s", original);
    
    
    	strip(original, stripped);
    return 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    original[i] is a char, not a char*, which is what strcmp expects. If you want to compare a slice of a string, take the address. Either:
    Code:
    strcmp ( &original[i], "a" )
    or
    Code:
    strcmp ( original + i, "a" )
    But chances are good that the code doesn't do what you think it does.

    >for(int i = 0;i < original[i]; i++){
    1) That's probably not C unless you're explicitly compiling as C99 because prior to C99, you couldn't declare a variable in the initialization clause of a for loop. Also, that condition looks extremely foogly. You might want to make sure that it's doing what you expect.

    >gets(original);
    Please don't use gets. fgets is much safer.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    What if I wanted to compare for whitespace, can I use the second parameter as a character,
    Code:
    '\n'
    Or would I need to convert that to a string?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What if I wanted to compare for whitespace
    It depends on how you want to compare and if you're searching for multiple whitespace characters or just a newline. Are you just searching for the next whitespace character?
    Code:
    const char *ws = " \n\t\r\v";
    char *p = original;
    
    while ( ( p = strpbrk ( p, ws ) ) != NULL ) {
      /* *p is the next whitespace character */
      ++p;
    }
    Replace strpbrk with strchr if all you're doing is looking for a newline. If you're just comparing each character as you see it and doing something different, a manual loop is easier:
    Code:
    for ( i = 0; original[i] != '\0'; i++ ) {
      if ( isspace ( original[i] ) ) {
        /* Do something with the whitespace */
      }
      else {
        /* Do something with non-whitespace */
      }
    }
    >can I use the second parameter as a character
    No, and for the same reason. The second parameter is expected to be a string. If it's not a string, it won't compile. If it's not a string and you trick the compiler into thinking it's a string, you've got broken code.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-22-2009, 02:35 AM
  2. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 06:30 PM
  3. Problem with simple C programming
    By YAV in forum C Programming
    Replies: 2
    Last Post: 11-17-2008, 10:29 AM
  4. strcmp problem, whats the problem, i cant figure it out!
    By AvaGodess in forum C Programming
    Replies: 14
    Last Post: 10-18-2008, 06:45 PM
  5. Replies: 5
    Last Post: 12-03-2003, 05:47 PM