Hash table in C always yields Seg fault -


i trying develop hash table in c programming language fails seg fault. trying seperate chaining created struct has 2 properties: word , next. word char* , next pointer next node, creating hash table conatains array of linked list.

typedef struct node  {     char* word;     struct node* next;  }node;  node* table[26];  

after indexing table using hashing function indexes table.

do have fix?

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <cs50.h>  typedef struct node  {     char* word;     struct node* next;  }node;  node* table[26];    int hash(char* key); void index();  int main(int argc, char* argv[]) {     index();      return 0;  }  int hash(char* key) {     int hash = toupper(key[0]) - 'a';     int res = hash % 26;      return res; }  void index() {        printf("insert word: ");     char* k = getstring();           node* predptr = malloc(sizeof(node));     node* newptr = malloc(sizeof(node));      for(int = 0; < 26; i++)     {         if(hash(k) == i)         {             predptr = table[0];              predptr->next = newptr;              newptr = predptr;              break;         }          else         {          }     }   } 

typedef struct node  {     char* word;     struct node* next;  }node;  node* table[26];  

table array of 26 pointers, initialized null.

specifically table[0] null pointer , try dereference it

            predptr = table[0];              predptr->next = newptr; // dereference null pointer             // null->next 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -