-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcallback_function.c
More file actions
63 lines (52 loc) · 1.41 KB
/
Copy pathcallback_function.c
File metadata and controls
63 lines (52 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// #include<stdio.h>
// void function1()
// {
// printf("I am Balkumar function1\n");
// }
// // callback function
// void function2(void (*ptr)())
// {
// (*ptr) (); // callback to function1
// }
// int main()
// {
// void (*ptr)() = &function1;
// // calling function function2 and passing
// // address of the function function1 as argument
// function2(ptr);
// return 0;
// }
// #include<stdio.h>
// void my_function() {
// printf("This is a normal function.\n");
// }
// void my_callback_function(void (*ptr)()) {
// printf("This is callback function.\n");
// (*ptr)(); //calling the callback function
// }
// main()
// {
// void (*ptr)() = &my_function;
// my_callback_function(ptr);
// }
#include<stdio.h>
int func(int, int);
int main(void)
{
int result1,result2;
/* declaring a pointer to a function which takes
two int arguments and returns an integer as result */
int (*ptrFunc)(int,int);
/* assigning ptrFunc to func's address */
ptrFunc=func; // passing function address
/* calling func() through explicit dereference */
result1 = (*ptrFunc)(10,20); // dereferencing of the function pointer
/* calling func() through implicit dereference */
result2 = ptrFunc(10,20);
printf("result1 = %d result2 = %d\n",result1,result2);
return 0;
}
int func(int x, int y)
{
return x+y;
}