My search function of a Linked List termintes mid-program!

This is a discussion on My search function of a Linked List termintes mid-program! within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <ctype.h> typedef struct N { char x[20]; struct N *next; } List; List *insertlist(char ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    145

    My search function of a Linked List terminates mid-program!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
     
     
    typedef struct N {
      char x[20];
      struct 	N *next;
    }
    List;
    
    List *insertlist(char word[15],List *b) {
      int i;
      List *c = calloc(1,sizeof(List));
      for(i=0;i<21;i++) {
        c->x[i] = word[i]; 
      }
      c->next = b;
      return c;
    
    
    }
    
    List *text(char *word) {
    List *d = NULL;
    d=insertlist(word,d);
    
    return d;
    }	  
    
     search(char *query,List *z) {
                 printf("pass");
         while( z!= NULL ) {
                if(z->x == query) {
                        printf("match");
                        }
                        z = z->next;
                        }
                        return 0 ;                  
                        }
                        
    
        
    
    
    int main(int argc, char *argv[]) {
        char word[15];
        char query[15];
        char x;
        List *z;
        int c;
        FILE *fp;
        fp=fopen(argv[1], "r");
     
        /* set things up first - get first character, set c=0*/
        c=0;
        x = fgetc(fp);
        while  (x!= EOF)
        {
          
            if (isspace(x) || ispunct(x) )
            {
                word[c]='\0';
                /*for testing only - prints out each word*/
                printf("%s\n", word);
                /*next char goes to start of word*/
                c=0;
    	    z = insertlist(word,z); 
    	    
            }
     
            else
            {
                word[c]=x;
                /*next char goes on end of word*/
                c=c+1;
            }       
                             
            /*get next character*/        
            x = fgetc(fp);
      }
    
        fclose(fp);
        
    
        
    
         
    
    
     
        printf("Please enter a search query:");
        fgets(query,15,stdin);
        printf("Scanned query\n");
        search(query,z);
    
        
    }

    I have put the search function in bold. The program will manage to print 'Scanned query' (also in bold), but then will just terminate. Can anybody give me some clues as to why it will not work?

    Thanks
    Last edited by Wiretron; 01-27-2006 at 12:18 PM.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    JUst a side note, when I say 'terminate', I mean it will quit unexpectedly, I'm running in Windows atm.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    1. for(i=0;i<21;i++)
    You run off the end of your array

    2. if(z->x == query)
    Use strcmp()

    3. Your formatting sucks.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Edited my search function to this:

    Code:
     search(char *query,List *z) {
                 int i = 1;
                 printf("pass");
         while( z!= NULL ) {
                i = strcmp(query,z->x) ;
                if (i = 0) {
                      printf("yay");
                      }
                        
                        z = z->next;
                        }
                        return 0 ;                  
                        }
    Still quits

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    I get the feeling that its this part of the code which causes it to quit

    Code:
     while( z!= NULL ) {
                i = strcmp(query,z->x) ;

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > if (i = 0)
    Out of one pan, and into another
    Use ==

    Better yet, get a compiler to warn you when you do use = when you mean ==

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Yeah thanks for that, it still crashes though.

  8. #8
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    typedef struct N {
      char x[20];              
      struct 	N *next;
    }
    List;
    
    List *insertlist(char word[15],List *b) { // why 15 ?
      int i;
      List *c = calloc(1,sizeof(List));
      for(i=0;i<21;i++) {  // should be 20
        c->x[i] = word[i]; 
      }
      c->next = b;
      return c;
    }
    Code:
    int main(int argc, char *argv[]) {
        char word[15];
    Kurt

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Changed all the 15's to 20's, but it still hangs.

  10. #10
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    have you changed the 21 too ?
    are you shure there is no word longer then 19 chars in the file ?
    are you shure opening the file succeeeds ?
    what if there are more then one spaces or punctation chars ?
    Kurt
    EDIT: x is a chr it cannot hold EOF.
    Last edited by ZuK; 01-27-2006 at 01:48 PM.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Watch the !! birdies....
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    typedef struct N {
        char      x[20];
        struct N *next;
    } List;
    
    List *insertlist(char word[15], List * b)
    {
        List *c = calloc(1, sizeof(List));
        strcpy( c->x, word ); /*!! use strcpy for simplicity */
        c->next = b;
        return c;
    }
    
    /*!! unused function */
    List *text(char *word)
    {
        List *d = NULL;
        d = insertlist(word, d);
        return d;
    }
    
    int search(char *query, List * z)   /*!! be explicit about return types */
    {
        printf("pass");
        while (z != NULL) {
            if ( strcmp(z->x,query) == 0 ) {
                printf("match");
            }
            z = z->next;
        }
        return 0;
    }
    
    int main(int argc, char *argv[])
    {
        char word[15];
        char query[15];
        int   x;        /*!! must be int to compare with EOF */
        List *z = NULL; /*!! an empty list */
        int c;
        FILE *fp;
    
        fp = fopen(argv[1], "r");
        /* set things up first - get first character, set c=0 */
        c = 0;
        while ( (x = fgetc(fp)) != EOF) {
            if (isspace(x) || ispunct(x)) {
                word[c] = '\0';
                /*for testing only - prints out each word */
                printf("%s\n", word);
                /*next char goes to start of word */
                c = 0;
                z = insertlist(word, z);
            } else {
                word[c] = x;
                /*next char goes on end of word */
                c = c + 1;
            }
        }
    
        fclose(fp);
        printf("Please enter a search query:");
        fgets(query, 15, stdin);
        printf("Scanned query\n");
        search(query, z);
        return 0;   /*!! needs a return result */
    }

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Thanks for that, how come it does not print 'match' though if I search for a word that is in the document?

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Gee, do I have to do the whole thing for you?

    How about trying to debug it a bit yourself - I stopped the thing crashing on you, all you have to do is dig a bit and figure it out.

    Hint.
    Code:
    int search(char *query, List * z)
    {
      printf("pass\n");
      while (z != NULL) {
        printf( "Comparing '%s' with '%s'\n", query, z->x );
        if ( strcmp(z->x,query) == 0 ) {
          printf("match\n");
        }
        z = z->next;
      }
      return 0;
    }
    Really big hint - read up on what fgets() stores and then read the FAQ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM

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