Click here to Skip to main content
15,891,920 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
What is the Advantage of Function Pointer?
Posted
Comments
Emilio Garavaglia 16-Sep-10 2:52am    
Said in this way, isa non-sense question. An "advantage" is a somwhat performance "difference", and to have a "difference" you need a reference to start with.

In more simple words: Advantage respect to what ??

I wouldn't label the use of function pointers in terms of
optimization or fast calls. Instead I style my understanding
of function pointers in the terms of pointer variables. In C a function,
by itself, is not a variable. Pointers to functions are variables and
can be used in assignments, arrays, passed to functions, etc.

Examples:

/* type definition of a function pointer */
typedef int (*FuncPointer)(void);

/* A function definition */
int foo(void)
{
return 6;
};

int bar(void)
{
return 10;
};

You cannot do this:
foo = bar; /* ERROR */

But you can do this:
FuncPointer p1,p2;
/* assignments */
p1 = foo;
p2 = bar;
p1 = p2;

/* array of function pointers */
FuncPointer parray[] = {foo,bar};
int d = parray[0]();

/* As a function parameter */
int Afunction(int i, FuncPointer p){return i*p();};
and the call:
int d = Afunction(2,foo);

/************ Code Example *********************/
#include <stdio.h>

typedef int (*FuncPointer)(void);

/* A function definition */
int foo(void)
{
return 6;
};

int bar(void)
{
return 10;
};

int Afunction(int i, FuncPointer p){return i*p();};

int main(void)
{
FuncPointer p1,p2;
FuncPointer parray[] = {foo,bar};
int d;

/* assignments */
p1 = foo;
p2 = bar;
printf("p2 returns %d\n",p2());

/* array of function pointers */
d = parray[0]();
printf("d = %d\n",d);

/* As a function parameter */
d = Afunction(2,foo);
printf("d = %d\n",d);
return 0;
}
/***********************END***************/


hope you can understand this properly
 
Share this answer
 
v2
Comments
Richard MacCutchan 16-Sep-10 4:35am    
If you must post sample code then at least format it properly according to the guidelines.
TiruKom 19-Jul-12 6:39am    
Please explain me about cross compiler.
One more answer

One of the big uses for function pointers in C is to call a function defined at run-time. For example, the C run-time library has two routines, qsort and bsearch, which take a pointer to a function that it calls to compare two items being sorted; this allows you to sort or search, respectively, anything, based on any criteria you wish to use.
I think the more common use of a function pointer is to generalise function calls. e.g., if there is one function called sum_gen(a, b) which in turn may require to call iself() function or isquare() which are of similar types then what we will do, we will add one function pointer argument to the sum_gen() function like:-
sum_gen(a, b, int (*fun)(void)), where fun can take either the address of iself() function or isquare function.
Now we can call sum_gen() function like:-
sum_gen(a, b, iself) or sum_gen(a, b, isquare). So its basically used to generalise the function calls.

They can be used to create 'jump table'.
enum { ADD,SUB,MUL,DIV };
double (*fptrs[])(double,double)={ add, sub, mul, divi };
(*fptrs[ operator ] ) (op1,op2);
Jump table might be more efficient and elegant than switch statement.

The other use is, when we want to call some functions sequentially one by one, in that case we declare one array of function pointers initialised to the respective functions like:-
unsigned int i ;
int (*fun[]) (void) = { fun1, fun2, fun3, fun4, fun5, fun6 };
for( i = 0; i < (sizeof (fun) / sizeof (fun[0])); i++)
(*fun [ i ])();
in this way all the function will be called sequentially instead of calling each function one by one.
Note that calling using (*f)() and f() where f is pointer to function is the same in ANSI-C. You might have f()() if f() is a function that returns pointer to function.



Hope this will we useful for you to understand do let me know.
 
Share this answer
 
Comments
Richard MacCutchan 16-Sep-10 4:36am    
See my comment above.
In C++ there's virtually (pardon the pun) no utility in using function pointers. Everything you can do with a function pointer you should consider using an interface pointer OR a functor instead.

The reason is that it's easier to partition state using a functor or object. Functions cannot carry state around with them - at least in a way that easily allows multiple copies of that state.

Cheers,

Ash

PS: This doesn't apply to member function pointers, they're very useful for doing double dispatch and other things that have to depend on two types.
 
Share this answer
 
Comments
ThatsAlok 16-Sep-10 7:17am    
You are right, however i have created pattern out of function pointers which similar to control array in VB
Aescleal 16-Sep-10 7:37am    
I've done similar things in my time - my unit test framework uses function pointers rather than interfaces. However if I were to do it again I'd (probably) implement an interface instead.
If develope some kind of command parser, functionpointer is is very useful as follow:

CSS
typedef struct
{
    char             *szCmd;
    CMD_FNP          fnp;
} T_CMD;


T_CMD gt_cmd[] = {
{ "CMD1", cmd_ABOR },
{ "CMD2", cmd_ACCT },
{ "CMD3", cmd_ADAT },
{ "CMD4", cmd_ALLO },
....
{ "CMDn", cmd_APPE } };


int Execute_Command( char *s )
{
char *argv[10];
int argc;
int idx;
int rc;

argc = Parse_Command( s, argv );
for( idx=0; idx<(sizeof(gt_cmd)/sizeof(T_CMD)); idx++ )
{
if( strcmpi( argv[0], gt_cmd[idx].cmd )
{
rc = gt_cmd[idx].fnp( argc, argv );
return( rc );
}
}

return( FALSE );
}

But if not use function pointer, you have to use switch or if ... else.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900