pointers to functions in c code example
Example 1: how to use a pointer as a parameter in c
#include
#include
#include
void add(int* a, int* b, int* c)
{
*c = *a + *b;
}
int main()
{
int a, b, c;
a = 3;
b = 5;
add(&a, &b, &c);
printf("%d", c);
}
Example 2: c function pointer
//Declaration of a pointer to a function that takes an integer
//and returns an integer.
int (*f_ptr)(int);
//Assignment of a function foo to the function pointer f_ptr declared above.
f_ptr = foo;
//Calling foo indirectly via f_ptr, passing the return value of foo to r.
int r = f_ptr(v);
//Assigning an address of a function to the function pointer f_ptr,
//then calling foo by dereferencing the function pointer.
f_ptr = &foo;
int r = (*f_ptr)(v);
Example 3: How to use pointers in C
myvar = 25;
foo = &myvar;
bar = myvar;