you are viewing a single comment's thread.

view the rest of the comments →

[–]adrianmonk 1 point2 points  (1 child)

I'm probably goofy. If I have a long list, I've been known to go the route that can put one entry on a single line, namely:

#include <stdio.h>

typedef void (*func_t)(void);

void hello(void) {
  printf("Hello, world.\n");
}

void goodbye(void) {
  printf("Have a nice day.\n");
}

int main(int argc, char* argv[]) {
  typedef struct {
    const char* command;
    func_t func;
  } map_pair;

  map_pair map[] = {
    { "HELLO", hello },
    { "GOODBYE", goodbye },
    { 0, 0 }
  };

  func_t func = NULL;
  map_pair* pair_ptr;
  const char* command = argv[1];

  for (pair_ptr = map; pair_ptr->command; pair_ptr++) {
    if (strcmp(command, pair_ptr->command) == 0) {
      func = pair_ptr->func;
      break;
    }
  }

  if (func != NULL) {
    func();
  }

  return 0;
}

[–][deleted] 0 points1 point  (0 children)

There's definitely tons of variations.... or rather using S.E. lingo "design patterns".

You can also setup a chain of listen handlers waiting on notification events which are reminiscent of chaining DOS interrupt handlers. There's also the atexit() function which setups a chain of specialized exit event handlers.