【问题标题】:How insert word into simple hash function C如何将单词插入简单的哈希函数C
【发布时间】:2019-07-27 10:02:02
【问题描述】:

Noob,试图弄清楚如何将 1 个单词插入一个简单的哈希表(已给出)。这个词是小写的。但是,我收到以下错误:

""exit status 1
main.c:30:40: error: function definition is not allowed here
    unsigned int hash(const char *word){""


#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>

int main(void) {
    int N = 26;
    // Length = 45;

    // Represents a node in a hash table
    typedef struct node
    {
        // LENGTH is defined in dictionary.h as 45, max length
        char word[45+1];
        struct node *next;
    }
    node;

    // Represents a hash table, N is defined as 26 above
    node *hashtable[N];

    // Initialize hash table
    for (int i = 0; i < N; i++)
    {
        hashtable[i] = NULL;
    }

    char *word = "jello";

    // Hashes word to a number between 0 (a) and 25 (z), inclusive, based on its first letter, creating 26 buckets, or alphabetized categories

    unsigned int hash(const char *word){
        return tolower(word[0]) - 'a';
    }

    int x = word[0] - 'a';

    printf("%d\n", x);
    printf("%s\n", word);
    printf("%c\n", word[0]);

    return 0;
}

【问题讨论】:

  • 函数定义应该在main(或任何其他函数)之外。
  • 轻松修复我的代码,谢谢

标签: c function hash compiler-errors


【解决方案1】:

正如错误消息所暗示的,您在不应该定义的地方定义了一个函数 - 在这种情况下,在 main 函数内。把它移出那里,你应该没事。

【讨论】:

    猜你喜欢
    • 2013-01-02
    • 1970-01-01
    • 2023-03-04
    • 2019-10-22
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    相关资源
    最近更新 更多