-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuncpointers.c
More file actions
69 lines (66 loc) · 1.77 KB
/
funcpointers.c
File metadata and controls
69 lines (66 loc) · 1.77 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
63
64
65
66
67
68
69
#include<stdio.h>
/*
* Function prototypes
*/
double add(double, double); /*return the sum of two parameters*/
double sub(double, double); /*return the difference of two parameters*/
double mul(double, double); /*return the product of two parameters*/
/*
* array op function
* Parameters: array of data values of type double, size of array,
* function pointer that returns double and accepts two values of
* type double
*/
double array_op(double [], int, double(*)(double, double));
/*
* add function: adds the two parameters
*/
double add(double a, double b){
return (a + b);
}
/*
* sub function: subtracts the two parameters
*/
double sub(double a, double b){
return (a - b);
}
/*
* mul function: multiplies the two parameters
*/
double mul(double a, double b){
return (a * b);
}
/*
* array_op function: performs pfun operation on the array
*/
double array_op(double array[], int size, double(*pfun)(double, double)){
int x = 0;
double ret = 0.0;
for(x = 0;x < size;){
double a = array[x++];
double b = array[x++];
ret += pfun(a, b);
}
return ret;
}
/*
* main function
*/
void main(){
double array[10] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
/*Declaring a pointer to a function*/
double(*pfun)(double, double);
double result = 0.0;
/*Calling array_op with add function*/
pfun = add;
result = array_op(array, 10, pfun);
printf("Result of array_op(array, 10, add) is %f\n", result);
/*Calling array_op with sub function*/
pfun = sub;
result = array_op(array, 10, pfun);
printf("Result of array_op(array, 10, sub) is %f\n", result);
/*Calling array_op with mul function*/
result = array_op(array, 10, mul);
printf("Result of array_op(array, 10, mul) is %f\n", result);
return;
}