Segment fault when i trying to write Dictionary in C code -


this code:

#include <stdio.h> #include <malloc.h> #include <stdlib.h> #include <string.h> #include <ctype.h>  struct node {     char data [ 20 ];     char m [ 20 ];     int mcount;     struct node * next; };  struct node * dic [26];  void add(char *); int search(char *); void show(); void deldic();  void main() {     char word [ 20 ], ch;     int i;        while (1) {          printf("\n\t\tdictionary\n");         printf("\n\t\t1.add word.\n");         printf("\t\t2.search word.\n");         printf("\t\t3.show dictionary.\n");         printf("\t\t4.update.\n");         printf("\t\t0.exit.");         printf("\n\n\t\tyour choice ");         scanf("%d", &ch);          switch (ch) {             case 1:                  printf("\nenter word : ");                 fpurge(stdin);                 gets(word);                 add(word);                  break;              case 2:                  printf("\nenter word search : ");                 fpurge(stdin);                 gets(word);                 = search(word);                 if (!i)                     printf("word not exists.");                   break;              case 3:                  show();                   break;              case 4:                 printf("\nenter word update: ");                 fpurge(stdin);                 gets(word);                 update(word);                 break;              default:                  printf("\nwrong choice");         }     } }  void add(char * str) {     int i, j = toupper(str [ 0 ]) - 65;     //      struct node * r, * temp = dic [ j ], * crawler;     char mean [ 20 ];      = search(str);     if (i) {         printf("\nword exists.");          return;     }     crawler = (struct node *) malloc(sizeof ( struct node));     strcpy(crawler -> data, str);     crawler -> next = null;     printf("\n\nenter meaning(s) : ");     gets(mean);     strcpy(crawler -> m, mean);     crawler -> mcount = i;     if (dic [ j ] == null || strcmp(dic [ j ] -> data, str) > 0) {         r = dic [ j ];         dic [ j ] = crawler;         crawler -> next = r;         return;     } else {         while (temp != null) {             if ((strcmp(temp -> data, str) < 0) && ((strcmp(temp -> next -> data, str) > 0) || temp -> next == null)) {                 crawler -> next = temp -> next;                 temp -> next = crawler;                 return;             }             temp = temp -> next;         }     } }  int search(char *str) {     struct node *n;     char temp1 [ 20 ];     char temp2 [ 20 ];     int i;      n = dic [ toupper(str [ 0 ]) - 65 ];     strcpy(temp2, str);     strupr(temp2);      while (n != null) {         strcpy(temp1, n -> data);          if (strcmp(strupr(temp1), temp2) == 0) {             printf("\n%s\t\t%s", n -> data, n -> m);             (i = 1; < n -> mcount; i++)                 printf("\n\t\t%s", n -> m);             return 1;         }         n = n -> next;     }     return 0; }  int update(char* str) {     struct node *n;     char temp1 [ 20 ];     char temp2 [ 20 ];     char mean [ 20];      n = dic [ toupper(str [ 0 ]) - 65 ];     strcpy(temp2, str);     strupr(temp2);      while (n != null) {         strcpy(temp1, n -> data);          if (strcmp(strupr(temp1), temp2) == 0) {             printf("\n\nenter meaning(s) : ");             gets(mean);             strcpy(n -> m, mean);             return 1;         }         n = n -> next;     }     return 0; } // print dictionary void show() {     struct node *n;     int i, j;      printf("word\t\tmeaning\n");     (i = 0; <= 30; i++)         printf("-");       (i = 0; <= 25; i++) {         n = dic [ ];         while (n != null) {             printf("\n%s\t\t%s", n -> data, n -> m);             (j = 1; j < n -> mcount; j++)                 printf("\n\t\t%s", n -> m);             n = n -> next;         }     } } //function free pointer. void deldic() {     struct node *n, *t;     int i;      (i = 0; <= 25; i++) {         n = dic [ ];         while (n != null) {             t = n -> next;             free(n);             n = t;         }     } } 

i'm using linked list write program.but when ran , enter 2 word :"go" , "goes" meaning.the error segmentation fault show up. had spent 1 hours it.difficult fix bug. need advice. reading.

the condition

(strcmp(temp->next->data, str) > 0) || temp->next == null 

is either buggy or superfluous. if temp->next can null, temp->next->data error. if can't, test not needed. perhaps wanted:

temp->next == null || (strcmp(temp->next->data, str) > 0) 

Comments