Stack Exchange
Segment fault when i trying to write Dictionary in C code
This is my 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 any word : ");
                fpurge(stdin);
                gets(word);
                add(word);

                break;

            case 2:

                printf("\nEnter the word to search : ");
                fpurge(stdin);
                gets(word);
                i = search(word);
                if (!i)
                    printf("Word does not exists.");


                break;

            case 3:

                show();


                break;

            case 4:
                printf("\nEnter any word to 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 ];

    i = search(str);
    if (i) {
        printf("\nWord already exists.");

        return;
    }
    crawler = (struct node *) malloc(sizeof ( struct node));
    strcpy(crawler -> data, str);
    crawler -> next = NULL;
    printf("\n\nEnter the 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);
            for (i = 1; i < 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 the meaning(s) : ");
            gets(mean);
            strcpy(n -> m, mean);
            return 1;
        }
        n = n -> next;
    }
    return 0;
}
// print the dictionary
void show() {
    struct node *n;
    int i, j;

    printf("Word\t\tMeaning\n");
    for (i = 0; i <= 30; i++)
        printf("-");


    for (i = 0; i <= 25; i++) {
        n = dic [ i ];
        while (n != NULL) {
            printf("\n%s\t\t%s", n -> data, n -> m);
            for (j = 1; j < n -> mcount; j++)
                printf("\n\t\t%s", n -> m);
            n = n -> next;
        }
    }
}
//function to free pointer.
void deldic() {
    struct node *n, *t;
    int i;

    for (i = 0; i <= 25; i++) {
        n = dic [ i ];
        while (n != NULL) {
            t = n -> next;
            free(n);
            n = t;
        }
    }
}

I'm using linked list to write this program.But when i ran and enter two word :"go" and "goes" with their meaning.The error Segmentation fault show up.
I had spent 1 hours to it.Difficult to fix that bug.
I need some advice.
Thanks for reading.

do you have access to gdb on your machine? it can tell you exact line segfault is happening on then go from there

It would help if you narrow it down to the function where it occurred, Additionally, if you are on Linux you may want to use valgrind.

@brad It's happening at gets(mean) when i enter "goes" and its meaning.

The condition
(strcmp(temp->next->data, str) > 0) || temp->next == NULL

is either buggy or superfluous. If temp->next can be null,  temp->next->data is an error. If it can't, the test is not needed. Perhaps you wanted:
temp->next == NULL || (strcmp(temp->next->data, str) > 0)

