search
Login

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

0 votes
472 views
asked Expert 13.2k points 26 40 57

1 Answer

0 votes
Declaration:

float (*fn[3])(int, int);

Program: Illustrates the usage of above declaration

#include<stdio.h>

float (*fn[3])(int, int);

float add(int, int);

int main() {

int x, y, z, j;

for (j = 0; j < 3; j++){

fn[j] = &add;

}

x = fn[0](10, 20);

y = fn[1](100, 200);

z = fn[2](1000, 2000);

printf("sum1 is: %d \n", x);

printf("sum2 is: %d \n", y);

printf("sum3 is: %d \n", z);

return 0;

}f

loat add(int x, int y) {

float f = x + y;

return f;

}

Output:

sum1 is: 30

sum2 is: 300

sum3 is: 3000

Explanation:

Here 'fn[3]' is an array of function pointers. Each element of the array can store the address of function 'float add(int, int)'. fn[0]=fn[1]=fn[2]=&add Wherever this address is encountered add(int, int) function is called.
answered Guru 39.1k points 10 21 32

Related questions

0 votes
1 answer 152 views
152 views
Write a program to accept three sides of a triangle and transfer them to a function to compute the area of a triangle.
asked anonymous
0 votes
1 answer 103 views
103 views
How do I declare an array of N pointers to functions returning pointers to functions returning pointers to char in c?
asked anonymous
0 votes
1 answer 455 views
0 votes
1 answer 29 views
29 views
asked john Guru 27.1k points 31 47 93
0 votes
1 answer 38 views
...