> Arindam Biswas <[EMAIL PROTECTED]> wrote:
> > Hi Friends,
> > I need to understand a very simple doubt:
> > Function pointer has some overhead associated with it.
So does addition, subtraction, etc...
> > Still WHY SHOULD we use function pointer instead of
> > calling directly that function.
Because sometimes you don't know which function is to
be called. Sometimes you don't want to know.
"Paul Herring" <[EMAIL PROTECTED]> wrote:
>
> There is no difference between a 'function' and a
> 'function pointer' except the syntax.
It's misleading to say that. They are different beasts.
One is a function, the other is an object.
One can have sizeof applied, the other can't.
> The function has parentheses after it:
>
> int foo(void){
> return 1;
> }
>
> int (bar*)(void) = foo;
typedef int (*func_ptr)(void);
func_ptr baz; /* fine */
func_ptr baz = foo; /* fine */
typedef int func(void);
func qux; /* valid function declaration */
func qux = foo; /* constraint violation */
> foo(); // calls foo
> bar(); // calls foo - no additional overhead from
> the line above.
There is the matter of sizeof bar bytes allocated for
the pointer, but the latter function call is the same
as the former because the former is the same as the
latter!
When you call a named function, the name decays to a
pointer.[1] In fact, the decay process for functions
is so extreme in C that the following is valid...
#include <stdio.h>
int main(void) { (*********puts("Hello"); return 0; }
[1] There is a parallel with arrays. But still, arrays
are not pointers, and functions are not function pointers.
--
Peter