0

I want to make a function to choose another one in C, maybe this C-pseudo code can help a little to clarify what I want:

void set_method(const char *method)
{
    // Check if the method is serial_port
    if (strcmp(method, "serial_port") == 0)
    {
        // assign the alias "print" to serial_print
        // something like defing here a function like this:
        // print(const char *print) { serial_print(print); }

        print(const char *print) = serial_print(const char *message)
    } 
    else if (strcmp(method, "graphical_tty") == 0)
    {
        // The same that serial_port case but with graphical_tty_print
    } 
    else
    {
        // Error
    } 
} 

The goal is to assign an "alias" to a function if the condition is met, how can I do this?

I'm using a freestanding C implementation for this, compiled with clang.

Jens
  • 69,818
  • 15
  • 125
  • 179
Rottenheimer2
  • 425
  • 1
  • 5
  • 13
  • are these 2 function have same signature? – user2736738 Jan 22 '18 at 15:13
  • @coderredoc understanding signature as prototype, yes, both have the same type and arguments, just the name changes. – Rottenheimer2 Jan 22 '18 at 15:15
  • Why not using a structure that hold the functions you need to use ? If I understand your problem, having a structure that hold pointers of function that you instantiate according to the 'object' that you have is a solution. It seems like you are trying to do object programming in C, so you would need to implement mechanism that are "hidden" in languages that are object oriented – Alexandre Pieroux Jan 22 '18 at 15:27

2 Answers2

6

It seems as you are looking for function pointers. See the following code, which introduces a type of function to call, a global pointer to a function of that type, and the code assigning an appropriate function according to your logic:

typedef int (*PrintFunctionType)(const char*);

int serial_print(const char *message) { printf("in serial: %s\n", message); return 0; }
int tty_print(const char* message) { printf("in tty: %s\n", message); return 0; }


PrintFunctionType print = serial_print;  // default

void set_method(const char *method)
{
    // Check if the method is serial_port
    if (strcmp(method, "serial_port") == 0)        {
        print  = serial_print;
    }
    else if (strcmp(method, "graphical_tty") == 0)        {
        print = tty_print;
    }
    else       {
        // Error
    }
}

int main() {
    print("Hello!");
    set_method("graphical_tty");
    print("Hello!");
}

//Output:
//
//in serial: Hello!
//in tty: Hello!

Hope it helps :-)

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
1

In case having same function signature you can use an array of function pointers. Example wise this would be like

typedef int (*fptr)(int);

fptr fa[5];
int f1(int);
int f2(int);    
fa[0]=f1;
fa[1]=f2;
....

Then based on comparison call any one of them. And pass this array to the function and call it's elements or even you can make this an global array and then use it in the function.

void set_method(const char *method, fptr fp[]){
    if (strcmp(method, "serial_port") == 0){
        (*fp[0])();
    }
    ...
}
user2736738
  • 30,591
  • 5
  • 42
  • 56