Passing an array of pointers to a function C program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows 2003 Server Standard Edition

Header file: Standard

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TP)

Other info: none

To do: Printing arrays' data using pointer variable and passing it to a function

To show: The relationship between an array of pointers and a function in C programming

 

 

/* An array of pointers to function */

#include <stdio.h>

 

/* function prototypes */

int fun1(int, double);

int fun2(int, double);

int fun3(int, double);

/* an array of a function pointers */

int (*p[3]) (int, double);

 

int main(void)

{

int i;

 

/* assigning address of functions to array pointers */

p[0] = fun1;

p[1] = fun2;

p[2] = fun3;

 

/* calling an array of function pointers with arguments */

for(i = 0; i <= 2; i++)

(*p[i]) (100, 1.234);

 

return 0;

}

 

/* function definition */

int fun1(int a, double b)

{

printf("a = %d b = %f", a, b);

return 0;

}int fun2(int c, double d)

{

printf("\nc = %d d = %f", c, d);

return 0;

}

 

int fun3(int e, double f)

{

printf("\ne = %d f = %f\n", e, f);

return 0;

}

 

Output example:

 

a = 100 b = 1.234000

c = 100 d = 1.234000

e = 100 f = 1.234000

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index