-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfunction_pointer_arrays.c
More file actions
40 lines (31 loc) · 855 Bytes
/
Copy pathfunction_pointer_arrays.c
File metadata and controls
40 lines (31 loc) · 855 Bytes
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
#include "stdio.h"
/*
* Description: Demo of array of function pointer.
*/
void add(int a, int b) {
printf("Addition of a and b is: %d\n", (a + b));
}
void substraction(int a, int b) {
printf("Substraction of a and b is: %d\n", (a - b));
}
void multiplication(int a, int b) {
printf("Multiplication of a and b is: %d\n", (a * b));
}
void division(int a, int b) {
printf("Division of a and b is: %d\n", (a / b));
}
int main(void) {
int ch;
void (*fun_ptr[])(int, int) = {add, substraction, multiplication, division};
printf(
"0. Add\n1. Substraction\n2. Multiplication\n3. Division\nEnter choice: ");
scanf("%d", &ch);
if ((0 > ch) || (3 < ch))
return -1;
else
fun_ptr[ch](10, 20);
printf("\nUsing loop to call all function sequentially");
for (ch = 0; ch <= 3; ch++)
fun_ptr[ch](10, 20);
return 0;
}