我刚刚通过cs50课程学习了c,我刚刚被介绍了指针和数据 struct (这很让人困惑,请帮助).因此,我得到了一个需要制作哈希表的项目,我首先try 将一些 node 添加到列表的零索引中,而不是立即使用哈希表,并且由于某种原因,我在将 node 添加到列表时出现了分段错误.Its位于第31行(即n->;next=表[0]->;next;)我无法理解为什么会发生这种情况.有人请帮忙并提前感谢
哈哈,我只是忘了添加代码
给你
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
typedef struct node
{
char *word;
struct node *next;
} node;
const unsigned int N = 10;
node *table[N];
for (int i = 0; i < 10; i++)
{
table[i] = NULL;
}
char *words[] = {"Hell", "Sup", "Brain", "Greek", "Mother", "Flip", "Poster", "Dark", "Apple", "Kandy"};
for (int i = 0; i < 10; i++)
{
char *wordle = words[i];
node *n = malloc(sizeof(node));
n->word = wordle;
n->next = table[0]->next;
table[0]->next = n;
printf("%s\n", table[0]->next->word);
}
}