Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4346480
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:10:43+00:00 2026-05-21T12:10:43+00:00

This is my first C program that does something useful. I’m implementing a generic

  • 0

This is my first C program that does something useful. I’m implementing a generic hash table that can take any type of key and item. But I’m getting errors I don’t know how to fix:
hash.h:32: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘hash_create’
hash.h:38: error: expected ‘)’ before ‘hash_func’
EDIT:
Aftertypedeffing struct hash_t to hash, I am getting tons of these errors.
hash.c:12: error: invalid type argument of ‘->’ (have ‘hash’)

#ifndef _HASH_H
#define _HASH_H

/* Definitions for abstract hashtable. */
typedef void * key;
typedef void * item;
typedef void * hash_func;
typedef void * compare_func;

struct pair {
    key k;
    item i;
};

struct hash_t {
    int size;
    int max_size;
    int array[16];
    hash_func hf;
    compare_func cf;
};
typedef int boolean_t;
#ifndef FALSE
#define FALSE 0
#define TRUE (!FALSE)
#endif // FALSE

/*
 * Creates and returns a new hashtable.
 * Returns null on failure, else a valid hashtable.
 */
extern hash_t hash_create();

/*
 * Sets the hash function to the new value if hash is empty.
 * Returns true if set successfully, false if nothing changed.
 */
extern boolean_t hash_set(hash_t, hash_func pf);

/* Sets the function used to compare values.
 * Returns true if set successfully, false if nothing changed.
 */
extern boolean_t hash_compare(hash_t h, hash_func pf);

/*
 * Returns TRUE if hashtable is empty. FALSE otherwise.
 */
extern boolean_t hash_is_empty(hash_t h);

/*
 * Returns number of elements in the table.
 */
extern int hash_size(hash_t h);

/*
 * Adds the key and value pair to hashtable.
 */

extern void hash_add(hash_t h, pair p);

/*
 * Returns the pair associated with the given key.
*/
extern pair hash_lookup(hash_t h, key k);

/*
  * Returns true if key is present, else false.
*/
extern boolean_t hash_is_present(hash_t h, key k);

/*
 * Removes the pair associated with given key.
 * Returns true if successfully removed, else false.
*/
extern boolean_t hash_remove(hash_t h, key k);

#endif //_HASH_H
</pre></code>

In case you want to look at the hash.c as well as the previous hash.h:
<pre><code>
/* Implements hash abstract data type. */ 
#include assert.h> //can't include < in post or it disappears
#include stdio.h> //can't include < in post or it disappears
#include stdlib.h> //can't include < in post or it disappears
#include "hash.h"

hash_t
hash_create()
{
    hash_t h = (hash_t)malloc(sizeof(struct hash_t));
    h->size = 0;
}

boolean_t
hash_set(hash_t, hash_func pf){
    if(!hash_is_empty)
        return FALSE;
    else
        hf = pf;
    return TRUE;
}

boolean_t
hash_set(hash_t, hash_func pf){
    if(!hash_is_empty)
        return FALSE;
    else
        cf = pf;
    return TRUE;
}

boolean_t
hash_is_empty(hash_t h){
    return h->size==0;
}

int
hash_size(hash_t h){
    return h->size;
}

void
hash_add(hash_t h, pair p) {
  int i = h->hash_func(max_size, p->key);
  if(h->array[i]!=NULL)
    i++;
  h->array[i] = p;
  h->size++;
  if(h->size > .75*h->maxsize) {
    h->max_size *= 2;
    int arr[h->max_size];
    int* temp = h->array;
    h->array = arr;
    int i = 0;
    for(i; i < h->max_size / 2; i++) {
      if(temp[i]!=NULL)
    hash_add(h,temp[i]);
    }
}

pair
hash_lookup(hash_t h, key k) {
  int i = h->hash_func(max_size, k);
  while(h->array[i]!=NULL && h->compare_func(h->array[i]->key,k)!=0) {
    i++;
  }
  return h->array[i];
}

boolean_t
hash_is_present(hash_t h, key k) {
  return (hash_lookup(h,k)!=NULL);
}

boolean_t
hash_remove(hash_t h, key k) {
  pair p = hash_lookup(h,k);
  if(p == NULL)
    return FALSE;
  else 
    *p = NULL;
  return TRUE;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T12:10:44+00:00Added an answer on May 21, 2026 at 12:10 pm

    Since you’re writing C you need to do:

    extern struct hash_t hash_create();
    

    hash_t isn’t a type – struct hash_t is. This applies to all other locations where you use hash_t as well – use struct hash_t instead.

    Alternatively:

    typedef struct hash_t {
        int size;
        int max_size;
        int array[16];
        hash_func hf;
        compare_func cf;
    } hash;
    
    extern hash hash_create();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python program that does something like this: Read a row from
this is my first question here so I hope I can articulate it well
This is my first crack at a method that is run periodically during the
I have a program that calls a Filewatcher function, like this Main() { watch();
problem euler #5 i found the solution but i don't know why this first
This is my first post here and I wanted to get some input from
This is my first experience using the Zend Framework. I am attempting to follow
this is my first question to stackoverflow so here it goes... I use cruise
This is my first time attempting to call an ASP.NET page method from jQuery.
This is my first time using joomla. I don't know if I'm using the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.