【发布时间】:2019-07-14 20:29:57
【问题描述】:
我正在开发 pset5 的哈希表版本。我无法将单词值分配给我创建的节点。在这个while循环之外我没有改变任何东西。我从以下行收到错误消息: node_ptr->word = word;它给出了一个错误“错误:数组类型'char [46]'不可分配。”为什么这条线不起作用?
// Buffer for a word
char word[LENGTH + 1];
// Insert words into hash table
while (fscanf(file, "%s", word) != EOF)
{
//1) Create a node
node *node_ptr = malloc(sizeof(node));
//check memory != NULL
if (!node_ptr)
{
return 1;
}
//assign values to node
node_ptr->word = word;
node_ptr->next = NULL;
【问题讨论】:
-
很难说没有看到
node是如何定义的。考虑使用minimal reproducible example。如果您尝试将数据从一个字符数组复制到另一个字符数组,请考虑strcpy。 -
一般
EOF不是fscanf的唯一故障模式。在您的情况下,这将导致文件中最后一个单词之后出现垃圾字符串。检查匹配参数的数量 (!= 1)