Thread: comparing pointers/strings

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    10

    comparing pointers/strings

    Hello, I have a problem of segment violation in the strcmp line.

    First I use "guardaFichero" and when i try to search the string with the function "buscaFichero" it fails, can you help me please?

    Code:
    char *ficherosLeidos[NUMMAXINCLUDES];
    
    ...
    
    void guardaFichero(char *fichero){
    
    ficherosLeidos[numFich]=malloc(strlen(fichero)+1);
    strcpy(ficherosLeidos[numFich],fichero);
    printf("Agregado el fichero: %s\n",ficherosLeidos[numFich]);
    
    }
    
    ....
    
    int buscaFichero(char *fichero){
    int i=0;
    
    	for(i=0;i<NUMMAXINCLUDES;i++){
    		if (strcmp(ficherosLeidos[i],fichero)==0) {
    			printf("Fichero repetido.\n");
    			return (1); 
    		}
    	}
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > for(i=0;i<NUMMAXINCLUDES;i++)
    Perhaps use numFich to limit the loop, rather than assuming all entries have been used.

    Also, do you check numFich < NUMMAXINCLUDES before doing this
    ficherosLeidos[numFich]=malloc(strlen(fichero)+1);
    (you should).
    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.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    10
    yeah, using numFich to limit the loop..it works. I thoght my problem was about pointers (I've forget all that stuff...).

    Thank You Salem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 10:44 PM
  3. comparing bitmaps
    By bigSteve in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 10:40 AM
  4. comparing problem
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 03-05-2002, 06:19 AM
  5. comparing struct members
    By breed in forum C Programming
    Replies: 4
    Last Post: 11-22-2001, 12:27 PM