Return Function Pointer based upon String I have the following function that takes a string and returns a function pointer; currently its a long if-else block; thats continuing to grow this will make it difficult to maintain. 

The logic of this function is repeated but the return type has changed. 

typedef int (*FP)(char** params, int numberOfParams); 
typedef int (*FP1)(int x); 

static FP getFunction1(char* name)
{
  if(strcmp(name, "str1") == 0)
  {
      return fun1; 
  }
  else if(strcmp(name, "str2") == 0)
  {
      return fun2; 
  }
}

static FP1 getFunction2(char* name)
{
  if(strcmp(name, "str1") == 0)
  {
      return fun3; 
  }
  else if(strcmp(name, "str2") == 0)
  {
      return fun4; 
  }
}


Is there a better way that this could be refactored to remove the duplicate code?
You could create a table mapping strings to functions:

typedef struct {
    char* name;
    FP    func;
} FP_mapping;

FP_mapping FP_map[] = {
    { "str1", fun1 },
    { "str2", fun2 },
    { NULL, NULL }
};


Then iterate over the array:

static FP getFunction1(char* name)
{
    for (int i=0; FP_map[i].name != NULL; i++)
    {
        if strcmp(FP_map[i].name, name)
        {
            return FP_map[i].func;
        }
    }
}