jacksparrow007
jacksparrow007

Reputation: 1328

hash table - linked lists - segmentation fault

I am trying to implement a hash table with linked list chaining. The following code below works -

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TABSIZ 200

struct record {
    struct record *next;
    char name[BUFSIZ];
    int data;
};

static struct record *htable[TABSIZ];

unsigned hash(char *s)
{
    unsigned h;

    for (h = 0; *s; s++)
        h = *s;
//printf("%d", h%TABSIZ);
//I know its not a good hash function but i wanted to check chaining
    return h % TABSIZ;
}

struct record *find(char *name)
{
    struct record *item;

    for (item = htable[hash(name)]; item; item = item->next)
    {
        if (strcmp(name, item->name) == 0)
            return item;
    }

    return NULL;
}

struct record *insert(char *name,int value)
{
    struct record *item;
    unsigned h;

    if ((item = find(name)) == NULL)
    {
        if ((item = malloc(sizeof (*item))) == NULL)
            return NULL;

        strcpy(item->name, name);
        item->data=value;
        h = hash(name);
        item->next = htable[h];
        htable[h] = item;
    }

    return item;
}
void printTable()
{
    int i=0;
    struct record *temp;
    for(i=0;i<=TABSIZ;i++)
    {
        temp=htable[i];
        while(temp!=NULL)
        {
            printf("\n%d - %s - %d\n", i,temp->name, temp->data);
            temp=temp->next;
            }
    }
}
int main(void)
{
    char buf[BUFSIZ];int value;
    struct record *item;
    do{
    printf("Enter the name of the student:\n");
    scanf("%s", buf);
    if(strcmp(buf,"stop")==0) break;
    printf("Enter the marks of the student:\n");
    scanf("%d", &value);
    if(insert(buf, value)==NULL)
    {
        break;
    }
}while((strcmp(buf,"stop"))!=0);

    printf("Enter a name to find: ");
    scanf("%s", buf);
    if((item=find(buf))!=NULL)
        printf("The marks of the student is %d\n", item->data);
    else printf("\n Not Found\n");
    printTable();
    return 0;
}

Now I am trying to remove the global variable and use local variable for the array of structures. I removed the global declaration of htable and declared it in main as

struct record *htable[TABSIZ];

and changed the functions to

struct record *find(struct record *htable, char *name);
struct record *insert(struct record *htable, char *name,int value);

and I'm calling the functions as

find(htable, name);
insert(htable,name,value);

but now my program is segfaulting. Am i passing the array of structures right? and have I declared it correctly. Any help would be greatly appreciated.

Upvotes: 0

Views: 609

Answers (2)

wildplasser
wildplasser

Reputation: 44250

In printTable():

for(i=0;i<=TABSIZ;i++)

looks suspect. You prabably want:

void printTable()
{
    unsigned int i;
    struct record *temp;

    for(i=0; i < TABSIZ;i++)
    {
        for (temp=htable[i]; temp!=NULL; temp=temp->next )
        {
            printf("\n%d - %s - %d\n", i,temp->name, temp->data);

        }
    }
}

Upvotes: 0

tomlogic
tomlogic

Reputation: 11694

I was going down the wrong path with my earlier answer.

When it's a global, it's automatically initialized to 0.

When it's declared on the stack of main, it's not initialized.

Add a memset( htable, 0, sizeof(htable)) in main(), and that should return it to the previous behavior.

Upvotes: 2

Related Questions