Advertisement
diegotoral

"anonymous structs"

Jun 18th, 2011
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.42 KB | None | 0 0
  1. int myfunc(char *str, int val)
  2. {
  3.  struct {
  4.    char  *name;
  5.    int   data;
  6.  } table[] = {
  7.    {"foo", 1},
  8.    {"bar", 2},
  9.    {"baz", 3},
  10.    {"abc", 4},
  11.    {"123", 5},
  12.    {NULL, NULL, 0}
  13.  };
  14.  static int DEFAULT_DATA = -1;
  15.  for (int i = 0; table[i].name; i++) {
  16.    if (strcmp(str, table[i].name) == 0) {
  17.      return some_fancy_function(val, table[i].data);
  18.    }
  19.  }
  20.  return some_fancy_function(val, DEFAULT_DATA);
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement