Programming in C: Unit III (b): Pointers

Array of Function Pointers

with Example C Program

When an array of function pointers is made, the appropriate function is selected using an index. The code given below shows the way to define and use an array of function pointers in C.

ARRAY OF FUNCTION POINTERS

When an array of function pointers is made, the appropriate function is selected using an index. The code given below shows the way to define and use an array of function pointers in C.

Step 1: Use typedef keyword so that 'fp' can be used as type

typedef int (*fp) (int, int);

Step 2: Define the array and initialize each element to NULL. This can be done in two ways:

// with 10 pointers to functions which return an int and take two ints

1. fp funcArr [10] = {NULL};

2. int (*funcArr [10]) (int, int) = {NULL};

Step 3: Assign the function's address-'Add' and 'Subtract' funcArr1 [0] = funcArr2[1] = Add;

funcArr [0] = &Add;

funcArr [1] = &Subtract;

Step 4: Call the function using an index to address the function pointer

printf("%d\n", funcArr [1] (2, 3));

// short form

printf("%d\n", (*funcArr [0]) (2, 3));

// correct way

30. Write a program to add, that uses an array of function pointers, subtract, multiply, or divide two given numbers.

#include <stdio.h>

int sum (int a, int b);

int subtract (int a, int b);

int mul (int a, int b);

int div (int a, int b);

int (*fp [4]) (int a, int b);

int main (void)

{

int result;

int num1, num2, op;

fp [0] = sum;

fp [1] = subtract;

fp [2] = mul;

fp [3] = div;

printf("\n Enter the numbers: ");

scanf("%d %d", &num1, &num2);

do

printf("\n 0: Add \n 1: Subtract \n 2: Multiply \n 3: Divide \n 4. EXIT\n");

printf("\n\n Enter r the operation: ");

scanf("%d", &op);

result (*fp [op]) (num1, num2);

printf("\n Result = %d", result);

} while (op!=4);

return 0;

}

int sum (int a, int b)

{

return a + b;

}

int subtract (int a, int b)

{

return a - b;

}

int mul (int a, int b)

{

return a* b;

}

int div(int a, int b)

{

if (b)

return a / b;

else

return 0;

}

Output

Enter the numbers: 2 3

0: Add

1: Subtract

2: Multiply

3: Divide

4. EXIT

Enter the operation: 0

Result = 5

Enter the operation: 4

Programming in C: Unit III (b): Pointers : Tag: : with Example C Program - Array of Function Pointers


Programming in C: Unit III (b): Pointers



Under Subject


Programming in C

CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation



Related Subjects


Professional English II

HS3251 2nd Semester 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation


Statistics and Numerical Methods

MA3251 2nd Semester 2021 Regulation M2 Engineering Mathematics 2 | 2nd Semester Common to all Dept 2021 Regulation


Engineering Graphics

GE3251 eg 2nd semester | 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation


Physics for Electrical Engineering

PH3202 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation


Basic Civil and Mechanical Engineering

BE3255 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation


Electric Circuit Analysis

EE3251 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation


Physics for Electronics Engineering

PH3254 - Physics II - 2nd Semester - ECE Department - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation


Electrical and Instrumentation Engineering

BE3254 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation


Circuit Analysis

EC3251 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation


Materials Science

PH3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation


Basic Electrical and Electronics Engineering

BE3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation


Physics for Civil Engineering

PH3201 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation


Basic Electrical, Electronics and Instrumentation Engineering

BE3252 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation


Physics for Information Science

PH3256 2nd Semester CSE Dept | 2021 Regulation | 2nd Semester CSE Dept 2021 Regulation


Basic Electrical and Electronics Engineering

BE3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation


Programming in C

CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation