How to pass a function pointer as an argument to a function in C

2 Answers

0 votes
#include <stdio.h>

void f(void (*p)()) {
    p();
}

void example() {
    printf("C Programming\n");
}

int main() {
     f(&example);
     
     return 0;
}
  
  
  
/*
run:
  
example
  
*/

 



answered Jun 10, 2024 by avibootz
0 votes
#include <stdio.h>

typedef int (*FUNC_PTR)(int, float);


void f(FUNC_PTR fptr) { 
    fptr(283, 3.14);
}

int example(int n, float f) { 
    printf("%f", n * f);
}

int main()
{
  FUNC_PTR fptr;
  
  fptr = example;
  
  f(fptr); 
  
  return 0;
}
  
  
  
/*
run:
  
888.620056
  
*/

 



answered Jun 10, 2024 by avibootz
...