C中的哈希表总是产生Seg故障

-1 c hash cs50

我正在尝试在C编程语言中开发一个哈希表,它总是因为seg错误而失败.我正在尝试单独链接,所以我创建了一个结构,它有两个属性:word和next.单词是char*,接下来是指向下一个节点的指针,最终创建一个包含链表列表的哈希表.

typedef struct node 
{
    char* word;
    struct node* next;

}node;

node* table[26]; 
Run Code Online (Sandbox Code Playgroud)

在此之后,我通过使用哈希函数索引到表中,该函数只是索引到表中.

你有修复吗?

#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 a word: ");
    char* k = GetString();    


    node* predptr = malloc(sizeof(node));
    node* newptr = malloc(sizeof(node));

    for(int i = 0; i < 26; i++)
    {
        if(hash(k) == i)
        {
            predptr = table[0];

            predptr->next = newptr;

            newptr = predptr;

            break;
        }

        else
        {

        }
    }


}
Run Code Online (Sandbox Code Playgroud)

pmg*_*pmg 5

typedef struct node 
{
    char* word;
    struct node* next;

}node;

node* table[26]; 
Run Code Online (Sandbox Code Playgroud)

table是一个由26个指针组成的数组,全部初始化为NULL.

具体table[0]NULL指针,你试图取消引用它

            predptr = table[0];

            predptr->next = newptr; // dereference NULL pointer
            // NULL->next
Run Code Online (Sandbox Code Playgroud)