Dave Hlave
Dave Hlave

Reputation: 55

C - Weird issue - expected identifier

I started using Ubuntu to get accommodated with Linux environments, and I've had this weird issue with my code, it gives me the following Error:

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

Here's the code:

#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;

}

I have no idea what the problem might be. I am practicing my Separate Chaining method on Hash Tables, it's not finished. Please help me resolve this, thank you.

Upvotes: 0

Views: 195

Answers (2)

Adrian Mole
Adrian Mole

Reputation: 51874

You have a number of errors in your code, most of which are in the definition of your find function. The comments in the 'corrected' code below should point these out:

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'.
}

There is also an error in your initialize function, similar to the last one mentioned in the code snippet I have shown above: that function must also return something.

Upvotes: 1

Astormooke
Astormooke

Reputation: 71

Quick glance, looks like there is a semi-colon at the end of line 60.

Upvotes: 1

Related Questions