【问题标题】:C - Weird issue - expected identifierC - 奇怪的问题 - 预期的标识符
【发布时间】:2020-11-29 17:39:11
【问题描述】:

我开始使用 Ubuntu 来适应 Linux 环境,但我的代码遇到了这个奇怪的问题,它给了我以下错误:

HashTable_SeparateChaining.c:60:1: error: expected identifier or ‘(’ before ‘{’ token
   60 | {
      | ^

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>

struct list_node
{

   struct list_node *next;
   char *key;
   char *value; 

};

struct hash_table
{

    int table_size;
    struct list_node **list_arr;

};

unsigned int hash(const char *key, unsigned int table_size);
struct hash_table *initialize(unsigned int table_size);
struct list_node *find(struct hash_table *H, const char *key);
void insert(struct hash_table *H, const char *key, const char *value);
void dump(struct hash_table *H);

unsigned int 
hash(const char *key, unsigned int table_size)
{
    unsigned long int hash = 0;
    for(int i=0;key[i];i++)
    {
        hash = (hash<<5) + key[i];
    }

    return (hash%table_size);
}


struct hash_table
*initialize(unsigned int table_size)
{

    struct hash_table *H = malloc(sizeof(*H));
    H->list_arr = malloc(sizeof(*H->list_arr)*table_size);

    /*H->table_size = table_size;*/

    for(unsigned int i = 0; i<table_size; i++)
    {
        H->list_arr[i] = malloc(sizeof(*H->list_arr[i]));
        H->list_arr[i]->next = NULL;
    }

}

struct list_node 
*find(struct hash_table *H, const char *key);
{

    unsigned int hash = hash(key,H->tablesize) ;
    struct list_node *current = H->list_arr[hash];

    /* more code... */

}

int main()
{   

    
    return 1;

}

我不知道问题可能是什么。我正在哈希表上练习我的分离链接方法,它还没有完成。请帮我解决这个问题,谢谢。

【问题讨论】:

  • 请不要用作为答案给出的代码更新您的问题。这样做会使答案完全无效。

标签: c linux data-structures compiler-errors


【解决方案1】:

快速浏览一下,好像第 60 行末尾有一个分号。

【讨论】:

  • 分号确实是一个错误……但它远非唯一一个!
【解决方案2】:

您的代码中有许多错误,大部分在您的 find 函数的定义中。下面“更正”代码中的 cmets 应该指出这些:

struct list_node
    * find(struct hash_table* H, const char* key)//; This semicolon is an error
{
    // You are giving a local variable the same name as a function you call...
    unsigned int hashx = hash(key, H->table_size);  // "tablesize" should be "table_size"
    struct list_node* current = H->list_arr[hashx]; // ... change `hash` to something else

    /* more code... */
    return current; // This function MUST return something - possibly 'current'.
}

您的initialize 函数中也有一个错误,类似于我上面显示的代码 sn-p 中提到的最后一个错误:该函数还必须返回 something

【讨论】:

  • 改了,还是一样的错误...我会把更新代码放上来看看。
  • @DaveHlave 好吧,当我对您的代码进行更改时,整个代码就可以正常编译了。 (关于有符号到无符号转换的一些警告,仅此而已。)在编译之前,您是否删除了分号并保存了文件?
  • @DaveHlave 如果您的代码中包含dumpinsert 函数,您可能会遇到类似的错误。但是您的问题中没有显示它们,因此很难确定。
  • 我很笨,我只是忘记在运行前保存...谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多