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 (*p[4]) (int x, int y);
int main(void)
{
int result;
int i, j, op;
p[0] = sum; /* address of sum() */
p[1] = subtract; /* address of subtract() */
p[2] = mul; /* address of mul() */
p[3] = div; /* address of div() */
[...]
要调用其中一个函数指针:
result = (*p[op]) (i, j); // op being the index of one of the four functions
void fun1()
{
}
void fun2()
{
}
void fun3()
{
}
void (*func_ptr[3])() = {fun1, fun2, fun3};
main()
{
int option;
printf("\nEnter function number you want");
printf("\nYou should not enter other than 0 , 1, 2"); /* because we have only 3 functions */
scanf("%d",&option);
if((option>=0)&&(option<=2))
{
(*func_ptr[option])();
}
return 0;
}
struct _GtkObjectClass
{
GInitiallyUnownedClass parent_class;
/* Non overridable class methods to set and get per class arguments */
void (*set_arg) (GtkObject *object,
GtkArg *arg,
guint arg_id);
void (*get_arg) (GtkObject *object,
GtkArg *arg,
guint arg_id);
/* Default signal handler for the ::destroy signal, which is
* invoked to request that references to the widget be dropped.
* If an object class overrides destroy() in order to perform class
* specific destruction then it must still invoke its superclass'
* implementation of the method after it is finished with its
* own cleanup. (See gtk_widget_real_destroy() for an example of
* how to do this).
*/
void (*destroy) (GtkObject *object);
};
#include <stdio.h>
#include "New_Fun.h"
int main()
{
int (*F_P)(int y);
void (*F_A[5])() = { F1, F2, F3, F4, F5 }; // if it is int the pointer incompatible is bound to happen
int xyz, i;
printf("Hello Function Pointer!\n");
F_P = fun;
xyz = F_P(5);
printf("The Value is %d\n", xyz);
//(*F_A[5]) = { F1, F2, F3, F4, F5 };
for (i = 0; i < 5; i++)
{
F_A[i]();
}
printf("\n\n");
F_A[f1]();
F_A[f2]();
F_A[f3]();
F_A[f4]();
return 0;
}
typedef int FUNC(int, int);
FUNC sum, subtract, mul, div;
FUNC *p[4] = { sum, subtract, mul, div };
int main(void)
{
int result;
int i = 2, j = 3, op = 2; // 2: mul
result = p[op](i, j); // = 6
}
// maybe even in another file
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) { return a/b; }
#include <stdio.h>
char * func1(char *a) {
*a = 'b';
return a;
}
char * func2(char *a) {
*a = 'c';
return a;
}
int main() {
char a = 'a';
/* declare array of function pointers
* the function pointer types are char * name(char *)
* A pointer to this type of function would be just
* put * before name, and parenthesis around *name:
* char * (*name)(char *)
* An array of these pointers is the same with [x]
*/
char * (*functions[2])(char *) = {func1, func2};
printf("%c, ", a);
/* the functions return a pointer, so I need to deference pointer
* Thats why the * in front of the parenthesis (in case it confused you)
*/
printf("%c, ", *(*functions[0])(&a));
printf("%c\n", *(*functions[1])(&a));
a = 'a';
/* creating 'name' for a function pointer type
* funcp is equivalent to type char *(*funcname)(char *)
*/
typedef char *(*funcp)(char *);
/* Now the declaration of the array of function pointers
* becomes easier
*/
funcp functions2[2] = {func1, func2};
printf("%c, ", a);
printf("%c, ", *(*functions2[0])(&a));
printf("%c\n", *(*functions2[1])(&a));
return 0;
}
void one( int a, int b){ printf(" \n[ ONE ] a = %d b = %d",a,b);}
void two( int a, int b){ printf(" \n[ TWO ] a = %d b = %d",a,b);}
void three( int a, int b){ printf("\n [ THREE ] a = %d b = %d",a,b);}
void four( int a, int b){ printf(" \n[ FOUR ] a = %d b = %d",a,b);}
void five( int a, int b){ printf(" \n [ FIVE ] a = %d b = %d",a,b);}
void(*p[2][2])(int,int) ;
int main()
{
int i,j;
printf("multidimensional array with function pointers\n");
p[0][0] = one; p[0][1] = two; p[1][0] = three; p[1][1] = four;
for ( i = 1 ; i >=0; i--)
for ( j = 0 ; j <2; j++)
(*p[i][j])( (i, i*j);
return 0;
}
void calculation(double result[] ){ //do the calculation on result
result[0] = 10+5;
result[1] = 10 +6;
.....
}
int main(){
double result[10] = {0}; //this is the vector of the results
calculation(result); //this will modify result
}
#include <iostream>
using namespace std;
int sum (int , int);
int prod (int , int);
int main()
{
int (*p[2])(int , int ) = {sum,prod};
cout << (*p[0])(2,3) << endl;
cout << (*p[1])(2,3) << endl;
}
int sum (int a , int b)
{
return a+b;
}
int prod (int a, int b)
{
return a*b;
}