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 6936533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:14:44+00:00 2026-05-27T12:14:44+00:00

I have programming experience with higher-level languages, and have started coding in plain C

  • 0

I have programming experience with higher-level languages, and have started coding in plain C a couple of weeks ago (for academic reasons). I want to implement a data structure something like a map<char,myStruct*>.

If that isn’t clear enough: I want a “mapping” for every possible SINGLE char onto a pointer to a structure I define somewhere else. If there was a way to ensure that no 2 chars can point to the same struct (without checking every other char when inserting a new element onto the map) that would be neat, but that is not strictly necessary. I also need to be able to remove pairings from the map, and to reinsert the pairings with the same Key but different pointers.

I have thought this through, and figured I could create a pointer array the length of all possible chars, and just store the corresponding pointer using the char as the array index (since it is just a number constant). This might very well work but it seems kind of inefficient to allocate that much space for addresses if I end up using only a couple of chars in my application.

Still, I wasn’t able to think of any alternative solutions (considering I’m a C newbie, not that surprising). I would be grateful for any, if even vague, suggestions in the right direction.

  • 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-27T12:14:45+00:00Added an answer on May 27, 2026 at 12:14 pm

    Just as you say (and as a commenter suggested), the easiest thing is to just do an array with a static size equal to the max value of the character data type:

    #include <limits.h>
    void * mapping[1u << CHAR_BIT];
    

    Assuming 64-bit pointers and 8-bit chars, this would occupy 8 * 256 = 2,048 bytes of memory for the entire map (excluding of course the “user data” which is what you store). For a program running on a 64-bit system, 2 KB of memory is trivial, and the ease of implementation and speed you get from this should balance the wasted memory quite nicely, in my opinion.

    The simplest thing to do if you still want to limit the “physical” size of the array would be to hash the single character, but then you need to start dealing with hash collision which immediately makes it more complicated.

    You could do something like:

    struct ValueChain
    {
      struct ValueChain *next;
      void *value;
      char key;
    }
    
    #define MAP_SIZE 127 /* This should be prime. */
    struct ValueChain* mapping[MAP_SIZE];
    

    Here we’ve halved the size of the pointer array, but the cost of each value has increased. Also you’re going to need dynamic allocations when inserting collided values.

    You could further compact it by doing e.g.

    #define MAP_SIZE 31
    struct ValueChain mapping[MAP_SIZE];
    

    Here each value in the array is a full ValueChain “list header”, rather than just a pointer to one. On a 64-bit machine this would probably use about 558 bytes for the mapping array, but you wouldn’t need to do any dynamic allocations until you detect a collision.

    The hashing for these could be just const char key = myChar % MAP_SIZE; to a first approximation, I guess.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning C, but i have a long experience with higher level programming languages
I have a programming experience with statically typed languages. Now writing code in Python
I have no programming experience related to building websites. I want to build a
I'm new to C# and don't have any programming experience. But I've finish a
Does anybody have experience programming for both the Intel Math Kernel Library and the
We have a number of electronic test technicians with next to no programming experience.
Warning: I have very little JavsScript experience. In my past programming experience, I usually
I have a two years of experience of programming in Visual C# and Visual
I do not have any experience with programming fractals. Of course I've seen the
Hi I have some experience with programming but I'm not very good with pointers.

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.