• C

Lookup Tables

I have been hearing the term lookuptables for a long time. What exactly is a lookup table, and how do you implement one in C? The only way I can think of is with if statments or using the switch block. I have looked through some of my old C books but there is no mention of lookup tables. Any info will be helpfull code would be great too.
Who is Participating?
mournbladeConnect With a Mentor Commented:
seems good. and you don't waste time finding what enum goes with what string. code is more efficient and much more readable. and no, i don't think it can be called a lookup table anymore :)

personnally, i would have make the fcn check_argument return the index and copy the values for pot and device in the place that fcn is called, and return -1 if not found. i.e.

#define NB_ENTRIES 16
int index;
if ((index = check_argument(argv[1])) != -1)
  pot = table[index].pot;
  device = table[index].device;
  /* ERROR! */

char check_argument(char *arg)
  for (int ii=0; ii < NB_ENTRIES; ii++)
    if (strcmp(table[ii].arg, arg) == 0)
      return ii;

  return -1;

more efficient, since you pass less parameters (smaller stack, less overhead) and, the table being a global var, you have access to its components everywhere. this is only personal taste however. do it the way you want :)
a lookup table is something really simple: it is a simple table implemented as an array or a linked list (for a variable number of possible answers). each of the elements in the array contains the answer related to a question that has the index as a parameter.

for example, if you want to know the number of bits set to one in a byte, you could have a lookup table (an array) of 256 int (int table[256]) that has the answer for each of the values a byte can have. e.g. if the byte you want to know the number of set bits has value 131 (10000011 in binary, 3 bits are set (i.e. have value '1'), you would look in the array at index 131 and search for your answer, which would be 3.

the table would have been filled previously with the following values:
table[127] = 7; /* (01111111) */
table[128] = 1; /* (10000000) */
table[129] = 2; /* (10000001) */
table[130] = 2; /* (10000010) */
table[131] = 3; /* (10000011) */
table[132] = 2; /* (10000100) */

so if you do a simple assignment like this:
int set_bits_131 = table[131];

set_bits would be equal to 3 and there's your answer.
simple, no? :)
SashoAuthor Commented:
I understand your answer completely mornblade. But my perticular situation is a bit more complicated. I have to use a command line option to assgin two values.


C:\vref v1_2

so based on the v1_2 i have to assign to values. I can just do:


But with many different options it is a nightmqre. So someone suggested to use a lookup table. I will not reject your answer but I hope you can help with my issue.
Managing Security Policy in a Changing Environment

The enterprise network environment is evolving rapidly as companies extend their physical data centers to embrace cloud computing and software-defined networking. This new reality means that the challenge of managing the security policy is much more dynamic and complex.

if the format of your argument is always 'vX_Y', why don't you parse the string to extract X and Y, convert them to integers and assign them to device and WCR? i.e. :

char *deviceStr;
char *WCRStr;
char *arg = (char*) malloc (sizeof(char) * (strlen(argv[1]) + 1));
int pos;

strcpy(arg, argv[1]);

/* starting at 1, not 0 to discard letter v */
/* search for '_'*/
for (pos = 1; arg[pos] != '_'; pos++);

arg[pos] = '\0'; /* separate the string in 2 */
deviceStr = (char*) malloc (sizeof(char) * pos);
strcpy(deviceStr, (arg+1)); /* will copy from X to '_'*/

/* here strlen() gives length from character after '_' to the end of arg */
WCRStr = (char*) malloc (sizeof(char) * (strlen(arg + pos + 1) + 1));
strcpy(WCRStr, (arg+pos+1)); /* will copy from Y to end of arg */

device = atoi(deviceStr);
WCR = atoi(WCRStr);


that works, i've tested it. and you don't need a lookup table.
SashoAuthor Commented:
Well the idea is good. But in my case
the argument vX_Y is not the way you see it X and Y are not device and WCR. Basically the argument will be a string and based on the string I will decide on what device and which WCR to use. I thought about your original idea and I guess I can implement what I need with a 3xN array where N is the number of possible arguments. So that each row will have the string, a device and a WCR. I am not really sure how to get mixed types in an array. I'll try to play around with it. Let me lnow what you think.
oops, my mistake. i shouldn't have jumped to conclusion that the 1s and 2s in the string and the values you want were the same things.

instead of an array 3xN use and array of N structures that contains your 3 types, i.e.

typedef struct
  char *arg;
  int device;
  int WCR;
} myStruct_t;

myStruct_t array[N];

and fill the array, the search by strcmp array[ii].arg with argv[1]

but then you have kinda the same problem that you had at the beginning because you have to actually fill the array, each element 1 by 1, and you needed a loop with strcmp to serach the array. it's not possible to do a lookup table with strings as a search parameter, you needed something that can be casted as an integer to be used as the index.
SashoAuthor Commented:
Yeah I just finished the implementation using an array of structs and I am filling the array one by one and having a loop using strcmp. Thats the best I could think of. I was thinking of enumerating the arguments but don't know how that will work.


enum args {v1_2,v1_8,v1_5}

to acess:

device =table[v1_2][0]
wiper  =table[v1_2][1]

the problem is converiting the string in argv[] to the enum type I am not really sure of how to do it.
#define table \
x(v1_2) \
x(v1_8) \
x(v1_5) \
/* end table */
#define x(arg)  arg,
enum args { table n_args };
#undef x
#define x(arg) # arg ,
char *strings[]={ table "undefined" };
#undef x
int convert(char *s){
  #define x(arg) if( strcmp( # arg , s ) == 0 ){ return arg; }
  #undef x
  return n_args;

SashoAuthor Commented:

would you mind disecting your code with some comments. I am trying to figure out what exactly it is doing It seems that it converts a string to a enum type? But I am not really sure. Is this the whole table implemenattion? I am lost
ozo's code actually works! i don't understand all of it but it actually converts a string to an int and then uses the int as an index to search the table and return the string to be printed out. i've never seen preprocessing used to such extent before.
hmm, i dissected ozo's code and i does exactly what you're doing with your code: it does a strcmp() on every first element of the array. the only difference is that the code is more compact and much more difficult to read.
SashoAuthor Commented:
Hmm...where would the device and wiper columns of the lookup-table be stored? And you are right the code is difficult to read. I think for code managment it will be a lot better to code it using what we mentioned earlier, after all I will have to move on to other projects and I don't want people who inherit ownership of my code to come to me every so often so that I can translate it to them. So far the implementation I have seems to work but I am not sure if it can be called a lookup table :)

struct lookup{
      char arg[5];
      unsigned char device;
      unsigned char pot;

struct lookup table[16];

void initialize_lookup_table()






unsigned char check_argument(char * arg,unsigned char * device,unsigned char * pot)
  int j;

            *device = table[j].device;
            *pot  = table[j].pot;
            return TRUE;

 return FALSE;

mornblade let me know what you think. Is there anything you could add?
SashoAuthor Commented:
I like your last comment as the answer for this whole issue. I'll try to accept it as the answer.
SashoAuthor Commented:
Well it seems the specs for this program of mine keep changing from day to day. This lookup table thing is getting out of hand. Now they want an otion to use a file as a lookup table initialization, I guess it is easier for the user to change the values. To me its just one more thing the user can screw up. Oh well...thanks for the help guys.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.