C Programs Tutorials | IT Developer
IT Developer

C Programming - C Lookup Tables



Share with a Friend

C Programming - C Lookup Tables

C Lookup Tables

A lookup table in C is a data structure (usually an array or hash table) used to map input values to corresponding output values. It allows for efficient data retrieval, often replacing repetitive computations or decision-making logic with simple indexing operations. Lookup tables are widely used in programming for tasks like function approximation, data mapping, and optimization.

Features of Lookup Tables

  1. Efficiency: Accessing data through indexing is faster than computing results dynamically.
  2. Precomputed Data: Values are precomputed and stored, reducing runtime computation.
  3. Space-Time Tradeoff: Increases memory usage but decreases computational overhead.

Types of Lookup Tables

  1. One-Dimensional Lookup Table:
    • A single array where an index directly corresponds to a value.
  2. Multi-Dimensional Lookup Table:
    • A multi-dimensional array used for more complex mappings.
  3. Hash Tables:
    • A more advanced form of lookup table using hash functions for data retrieval.
  4. Function Pointers Table:
    • A table of function pointers used to call different functions dynamically.

Basic Example of a Lookup Table

Using an Array

C

#include <stdio.h>

// Lookup table for square values

int square_lookup[10] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81};

int main() {

    int index = 7;

    printf("The square of %d is %d\n", index, square_lookup[index]);

    return 0;

}

Output:

The square of 7 is 49

Applications of Lookup Tables

  1. Mathematical Computations:
    • Storing precomputed values like sine, cosine, or logarithms.
  2. Data Mapping:
    • Converting input values to specific outputs (e.g., ASCII character tables).
  3. Function Pointers:
    • Dynamic function calls based on input.
  4. Encoding and Decoding:
    • Converting data to another format (e.g., Base64 encoding).
  5. Graphics and Image Processing:
    • Mapping colors or pixel intensities.

Advanced Examples

Function Pointer Table

C

#include <stdio.h>

// Functions

void function1() { printf("Function 1 called\n"); }

void function2() { printf("Function 2 called\n"); }

void function3() { printf("Function 3 called\n"); }

int main() {

    // Lookup table of function pointers

    void (*function_table[3])() = {function1, function2, function3};

    // Call a function using the table

    int choice = 2; // 0-based index

    if (choice >= 0 && choice < 3) {

        function_table[choice]();

    } else {

        printf("Invalid choice\n");

    }

    return 0;

}

Output:

Function 3 called

Character Mapping Table

C

#include <stdio.h>

// Simple ASCII to uppercase mapping

char uppercase_lookup[256];

void initialize_table() {

    for (int i = 0; i < 256; i++) {

        if (i >= 'a' && i <= 'z') {

            uppercase_lookup[i] = i - 32; // Convert to uppercase

        } else {

            uppercase_lookup[i] = i; // Keep as is

        }

    }

}

int main() {

    initialize_table();

    char input = 'g';

    printf("Uppercase of '%c' is '%c'\n", input, uppercase_lookup[(unsigned char)input]);

    return 0;

}

Output:

Uppercase of 'g' is 'G'

Advantages of Lookup Tables

  1. Fast Access: Data retrieval is instantaneous through indexing.
  2. Simplified Logic: Eliminates complex decision-making logic.
  3. Optimized Performance: Reduces runtime computation overhead.

Disadvantages of Lookup Tables

  1. Memory Usage: Requires additional memory to store precomputed values.
  2. Static Nature: Values are fixed unless explicitly updated.
  3. Limited Flexibility: Dynamic computation is not possible with static tables.

Conclusion

Lookup tables are a powerful tool for optimizing performance by replacing complex calculations or logic with fast data retrieval. By precomputing and storing values, they enhance efficiency, especially in real-time systems and applications requiring high-speed data access.