-1

I have a struct with a function pointer, that is intended for pointing to the bar() function, but i have no clue how to call the function that's being pointed on:

#include <stdlib.h>
typedef struct Foo
{
  int (*func_ptr)(const char *, const char *);
} Foo;

int bar(const char *a, const char *b)
{
  return 3;
}

int main(void)
{
  Foo *foo = (Foo *)malloc(sizeof(Foo));
  foo->func_ptr = &bar;
  //how do i call the function?
  return 0;
}
czarson
  • 125
  • 7

1 Answers1

1

Imagine your const char *a is "hello" and const char *b is "there", then you can use any of the following forms to call the function by its pointer:

  (foo->func_ptr)("hello", "there");
  (*foo->func_ptr)("hello", "there");
  foo->func_ptr("hello", "there");
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
AKL
  • 1,367
  • 7
  • 20