/* Demo: Lookup table. Compile: gcc -o table table.c */ #include #include #include #include #include // Table. Column1 and column2 are of type char struct { char *in; char *out; } cmd[] = { { "hello", "hi there" }, { "goodbye", "see you later" }, { "llama", "duck!" }, { "camel", "bactrians4lfe" }, { NULL, NULL }, }; int main (int argc, char *argv[]) { int i; if (argc < 2) printf("usage: %s \n"); // Loop through each row of cmd[] comparing argv[1] to // cmd[].in and print the matching cmd[].out on successful // match. We stop looping when cmd[i].in = NULL for (i=0; cmd[i].in; i++) if (!strcmp(argv[1], cmd[i].in)) { printf("%s\n", cmd[i].out); exit(0); } }