C Open Addressing

Intermediate
6 min read
Updated: Nov 2026
1 example

Probing for a Free Slot

In open addressing every key lives directly in the table array. On collision the algorithm probes other slots in a fixed sequence. With linear probing it tries i, i+1, i+2, ... modulo the table size. Search follows the same sequence and stops at the key or an empty slot. The table must never fill completely, so keep the load factor low.

Remember
for (p = 0; p < SIZE; p++) {
    s = (hash + p) % SIZE;
    if (t[s] == EMPTY) { t[s] = key; return s; }
}

Cheat Sheet

Keep these facts handy while you work with Open Addressing in C.

TopicDetail
Probe(h + p) % size
Stop (search)Key found or EMPTY
DeleteTombstone
LoadKeep under about 0.7

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Load Factor and Rehashing.

Example 1 — Linear probing

Keys 10, 17, 24, 3, 5 in a table of 7.

C
#include <stdio.h>
#define SIZE 7
#define EMPTY (-1)

static int insert(int t[], int key) {
    for (int p = 0; p < SIZE; p++) {
        int s = (key % SIZE + p) % SIZE;
        if (t[s] == EMPTY) { t[s] = key; return s; }
    }
    return -1;
}

static int search(const int t[], int key, int *probes) {
    *probes = 0;
    for (int p = 0; p < SIZE; p++) {
        int s = (key % SIZE + p) % SIZE;
        (*probes)++;
        if (t[s] == key) return s;
        if (t[s] == EMPTY) return -1;
    }
    return -1;
}

int main(void) {
    int t[SIZE], probes;
    for (int i = 0; i < SIZE; i++) t[i] = EMPTY;

    int keys[] = {10, 17, 24, 3, 5};
    for (int i = 0; i < 5; i++) insert(t, keys[i]);

    printf("table:");
    for (int i = 0; i < SIZE; i++) {
        if (t[i] == EMPTY) printf(" -");
        else printf(" %d", t[i]);
    }
    printf("\n");

    if (search(t, 24, &probes) >= 0) printf("search 24: found in %d probes\n", probes);
    if (search(t, 12, &probes) < 0) printf("search 12: not found after %d probes\n", probes);
    return 0;
}

How It Works

  1. 10, 17, 24, and 3 all hash to slot 3, so they fill slots 3, 4, 5, 6.
  2. Key 5 hashes to slot 5, finds 5 and 6 taken, and wraps to slot 0.
  3. Searching 12 starts at slot 5 and probes 5, 6, 0, then stops at the empty slot 1.

Edge Cases & Pitfalls

Check these before relying on Open Addressing in real programs.

full

Full table

Probing a full table loops forever without the p < SIZE limit.

del

Deleting without tombstones

Emptying a slot breaks searches for keys placed after it.

Key Takeaways

  • Keys live in the array.
  • Probe sequence is deterministic.
  • Keep the table sparse.

One line: In open addressing every key lives directly in the table array.

Frequently Asked Questions

A collision strategy that stores all entries in the table array and probes alternative slots when the preferred slot is taken.
Probing slots sequentially (i, i+1, i+2, ...) with wraparound until a free slot is found.
Continue with "C Load Factor and Rehashing" next. Resize and rehash when the load factor passes a threshold. The C Hash Tables hub lists every topic in order.

Next: Load Factor and Rehashing

Resize and rehash when the load factor passes a threshold.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful