Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a dictionary in C?

Tags:

c

dictionary

I'm programming a microcontroller in C and as part of it want to display certain letters on a 7 segment display. Each letter has a corresponding number that makes the 7 segment display show the letter. There's no real pattern to it cause the number is just made by adding up the bits on the 7 segment display that are needed to show the letter so it'd be really nice if I could create some sort of dictionary to do this.

If I was using C# or something I'd just make a dictionary and then add the letters as keys and the numbers as values but as far as I know I can't do this in C. Is there another way to do it or do should I just write a function like int displayletter(char letter) that uses a bunch of if statements to return the right numbers?

like image 662
Sam Avatar asked Oct 15 '10 07:10

Sam


1 Answers

You could create an array

 int values[26];

and populate it with the values for each letter, however they're calculated

Then create a function that takes a character and returns an int

int GetValueFromChar(char c)
{
    return values[c - 'A'];
}

This is simplistic, as it assumes you'll only use upper case letters in an ASCII character set, but you should get the idea.

like image 145
Matt Ellen Avatar answered Oct 14 '22 02:10

Matt Ellen