Click here to Skip to main content
Click here to Skip to main content

Simple C Hashtable

, 5 May 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple key value-pair hashtable written in C


This is a simple key-value pair hashtable written in C, uses FNV-1 as default hash algorithm. 

Using the code 

You can see an extended example of how to use this hashtable in the file main.c. 


// First, declare the hashtable
hashtable_t tbl;

// Then, call hashtable_init with the hash function and the len function
// In this example we will use strings as key
hashtable_init(&tbl, hashtable_hash_fnv, (hashtable_len_function)strlen); 

Adding elements 

// The key and the value can be of any type(int, string, structures...)
hashtable_set(&tbl, "key", "value"); 

Removing elements 

hashtable_remove(&tbl, "key"); 

Clearing the table 


Finally, destroy the table(actually, this just call hashtable_clear


Points of Interest 

This hashtable favors neither search neither insertion. If you want a search favored hashtable you must sort the elements. 


05/05/2012 - First version. 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jonathan Lima
Brazil Brazil
No Biography provided
Follow on   Twitter

Comments and Discussions

GeneralHashtable? PinmemberMember 962913325-Nov-12 8:09 
This really doesn't have to do anything with a hashtable… Finding an object in this hashtable means going through all the items in the array and checking the hash key - it's more of an accelerated array and not even that as the hash tables are often not very dense… Moreover it's buggy as it doesn't handle hash collisions.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150326.1 | Last Updated 5 May 2012
Article Copyright 2012 by Jonathan Lima
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid