I would like to pass variable 'c' from the main to (an arbitrary) function A using callback function B. As indicated, I can pass the variable 'b' to function A within function b, but I cannot find a syntax to pass from main.
#include<stdio.h>
void A(int a)
{
printf("I am in function A and here's a: %d\n", a);
}
// callback function
void B(void (*ptr)(int b) )
{
// int b = 5;
int b;
printf("I am in function B and b = %d\n", b);
(*ptr) (b); // callback to A
}
int main()
{
int c = 6;
void (*ptr)(int c) = &A;
// calling function B and passing
// address of the function A as argument
B(ptr);
return 0;
}
Is there a syntax to pass a variable from main to an arbitrary function using a call back?