Q:

call a function inside argument of another function c

int main (void) {
  Repeat(SayHello,4);
  Repeat(SayGoodbye,2);
}

void Repeat(void (*fct)(int), int i) {
  for (int j=0, j<i; j++) {
    (*fct)(j);
  }
}

void SayHello(int nbr) {
  printf("Hello n.%d\n", nbr);
}
void SayGoodbye(int nbr) {
  printf("Goodbye n.%d\n", nbr);
}

Result :
Hello n.0
Hello n.1
Hello n.2
Hello n.3
Goodbye n.0
Goodbye n.1
1
void func ( void (*f)(int) ) {
  for ( int ctr = 0 ; ctr < 5 ; ctr++ ) {
    (*f)(ctr);
  }
}
1
void func ( void (*f)(int) );
1

New to Communities?

Join the community