It is currently Tue, 30 Nov 2021 16:56:02 GMT

Author Message
 function calls versus function pointers ?
So, ehrm, what's the difference between a function pointer and just calling
that function ? I seem to
have understood they essentially do the same thing.

But i must be wrong.

Thank you.

 Sun, 14 Aug 2005 20:58:52 GMT   
 function calls versus function pointers ?
On Wed, 26 Feb 2003 13:58:52 +0100, in comp.unix.programmer, "test"

A function is a piece of executable logic. You may use a function on the
right side of an assignment, which causes the return value generated by a(n
implicit) call to the function to be used by the assignment in place of the
function. A function (function call) may not be used on the left side of an

OTOH, a function pointer is just a variable that contains the address of a
function. If used (without adornment) on the right side of an assignment,
it resolves to an address. It may also be used (with adornment) on the
right side of an assignment to obtain the return value that the function
would generate, just like a regular function call. It may also be used on
the left side of an assignment, to accept the address of a function.


int somefunction(int, int);     /* prototype for a callable function */

int (*funcpointer)(int, int);   /* a pointer to a function */
int (*computed_func)(int, int); /* a pointer to a function */

int computed_result;            /* a computed result */

/* when using a function... */
computed_result = somefunction(1,2);   /* legal - use of function */

/* This is illegal...
somefunction(0,0) = 9;

/* When using a function pointer */
funcpointer   = somefunction;          /* legal - fnptr as lvalue */
computed_func = funcpointer;           /* legal - fnptr as rvalue *
computed_result = *(funcpointer)(1,1); /* legal - fnptr as fn call */

Lew Pitcher, Information Technology Consultant, Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employer's.)

 Sun, 14 Aug 2005 21:22:57 GMT   
 function calls versus function pointers ?

A function pointer is (no surprise) a pointer to a function.  That
means that if you call a function pointer, you call the function that
the pointer points to.  Changing the value of the pointer will change
which function is called.

M?ns Rullg?rd

 Sun, 14 Aug 2005 21:19:24 GMT   
 function calls versus function pointers ?

Thanks both, but *what* use is it as in oppose, a normal function call.
seems i can do anything with a function call that i could do with a function

 Sun, 14 Aug 2005 21:34:14 GMT   
 function calls versus function pointers ?
In article <3e5cc2c2$0$19493$>,

man qsort

 Sun, 14 Aug 2005 21:56:25 GMT   
 function calls versus function pointers ?

It's used when you need to determine at runtime which function should
be used.  It is more convenient than cluttering the code with
if-statements.  A typical use is when you test for some particular cpu
instruction and use an optimized function if the instruction exists
(think of mmx).

M?ns Rullg?rd

 Sun, 14 Aug 2005 21:48:26 GMT   
 function calls versus function pointers ?

In article <3e5cba77$0$20243$>, test
( says...

They *are* essentially the same thing. If you consider how function
pointers are used then the reason for using them becomes more evident.

Consider a loop that runs a large number of times. Also consider that
inside this loop that a function must be called; but the function to be
called is determined by the evaluation of a complex logical expression
whose result does not change during the loop. Why bother re-evaluating
the expression each time the loop executes? Just evaluate the expression
once, outside the loop and assign the address of the function to be
executed to a function-pointer variable and then just call the desired
function inside the loop by using the function-pointer variable.

Another common usage of function pointers is a command lookup table.
An array of structures defines what function will be called with respect
to the name of the command entered by the user.


Barry Kimelman
Winnipeg, Manitoba, Canada
email :

 Sun, 14 Aug 2005 22:31:28 GMT   
 function calls versus function pointers ?

I get this. Move the evaluation out of the loop and use the function pointer

Not sure i get what you said here.
But still, are these the only uses for a function pointer ?

Thanks a lot!

 Mon, 15 Aug 2005 01:16:29 GMT   
 function calls versus function pointers ?

1) Function pointers are useful for callbacks

In OpenGL, for example, or GUI apis, you can do something like this:


This tells GL that when some event occurs (such as resizing the window),
then "my_function" should be called.  So, you pass a pointer to my_function
(as above) and OpenGL can just call this function.

2) Function pointers can be used in portable code and abstraction

For example, in kernel code we might have a layer of abstraction that
we add (like a virtual filesystem layer).  We can add function pointers
to a struct, something like:

struct file_ops
    int (* open)(inode *inode);
    int (* close)(inode *inode);
    int (* read)(inode *inode);

Obviously, each file system (ext2, NTFS, UFS, etc.) will open, close,
and read files differently; thus each will have its own function to
do so.  So, if we add this layer of abstraction and point these
generic open, read, and close functions to the REAL (ext2/etc.) functions,
then it's invisible to the caller what filesystem they're working with,
thus increasing portability and making for a cleaner interface.  So,
function pointers come in handy here (see linux kernel code for some
examples of this).

 josh(at)  |
 CS @ College of Computing, Georgia Institute of Technology, Atlanta
 373080 local keystrokes since last reboot (35 days ago)

 Mon, 15 Aug 2005 01:14:07 GMT   
 function calls versus function pointers ?

First, note that  when you define a function,  the compiler allocate a
memory range  to hold  the body of  the function, the  instructions it
compiled, and  then it keeps  a pointer to  this memory block  of code
under the name of the function.

So  when you  call the  function,  you are  actually referencing  this

    int f(int x)

    void main(void)
        f(2);    /* Here, f represents the address of the compiled
                    code of the body of f. For example, 0x8048420  */

$ gdb f
(gdb) x/20x f
0x8048420 <f>:      0x8be58955  0xe0830845  0x74c08501  0x08558b13
0x8048430 <f+16>:   0xc001d089  0x508dd001  0x08558901  0xf68915eb
0x8048440 <f+32>:   0x9908458b  0xe9c1d189  0x01148d1f  0xf8d1d089
0x8048450 <f+48>:   0x8b084589  0xd0890855  0xb68d06eb  0x00000000
0x8048460 <f+64>:   0xc35dec89  0x0000b68d  0xbf8d0000  0x00000000

Only that the symbol 'f' is a constant symbol, like:

    const int i=42;

then you could use i, but you could not assign to i.

Now when you can declare a variable function, ie. a variable holding a
variable pointer to  a function, you can do a lot  of tricks that were
done  in  the  old times  with  self  modifying  code,  but now  in  a
structured maner.

For example, you can the implement objects.  See the example about the
file system.  What you have there is a structure with a set of pointer
to functions.   This is actually a  set of methods on  the file system
object.   Object oriented language  just add  some syntactic  sugar to
hide  this  and  to  automatically  pass the  pointer  to  the  object
structure to the methods (and some object oriented languages add a few
additional semantics),  but the basics  are there, having  pointers to
different   functions  to  implement   different  behavior   for  each

Otherwise, they are  used as said for call back  functions, that is to
give a pre-written  library an entry point to your  code, which can be
called   "asynchronously".     See   for   example,    signal(2),   or
pthread_create(3), atexit(3), or qsort(3) as already cited.

That is, a function pointer variable allow a body of code to implement
the hook pattern, where some  internal function is not predefined, but
rather will be implemented by the client code.

The uses  a function  variable are only  limited by  your imagination,
like the use of the other kind of variables...

Note that  mathematically, there is  no difference between  a function
and any other kind of mathematical object.

Definition of an integer: the  quotient set of the couples of naturals
and the relation E such as: E( (a,b), (c,d) ) <=> a+d = c+b

Definition of a function from S to T: a subset of the power set of S x T.

So  everytime you  use  some data,  you  may want  to  use a  function
instead,  and vice-versa. With  function variables  (variables holding
pointers to function), you can do it if you need it.

For  example, it  may takes  much  less space  to have  a function  to
generate  the  data you  need.  Think  about  MP3, DivX,  etc.   These
compressions  consist in  transforming  the big  data  streams into  a
program that is interpreted by the decoder to regerate (similar) data.

So, instead  of having  a display function  taking an array  of pixels

    display(int pixels[])

you have a display function taking a decoder function:


There is a fault in reality. Do not adjust your minds. -- Salman Rushdie

 Mon, 15 Aug 2005 03:19:00 GMT   
 function calls versus function pointers ?

You can store the pointer into a variable, pass it to other functions, etc.
Another piece of the application may later call the function through
the pointer value, knowing the function signature but not knowing exactly
what function is being called.

 Mon, 15 Aug 2005 08:54:59 GMT   
 function calls versus function pointers ?

Calling a function is resolved at build time. Calling a function
pointer isn't.

 Fri, 19 Aug 2005 06:44:50 GMT   
 function calls versus function pointers ?

Let's say you're writing an interpreter. You can setup a table that
maps commands to functions which implement the commands. Then, you can
write a generic loop that determines what the command is, looks it up
in the table, and calls the function pointer to execute the command.

Or, suppose you have an application that loads a dynamic shared object
to carry out some processing. The module is determined and loaded at
runtime, so you can't just call functions directly. However, you can
use dlsym() to find the address of functions you want to call, and
invoke them through those pointers.

 Fri, 19 Aug 2005 07:15:32 GMT   
 function calls versus function pointers ?

Another example is callback functions. You can't tell in advance which
function should be called for which event.

 Fri, 19 Aug 2005 07:51:51 GMT   
 function calls versus function pointers ?

You can't resolve a call to a function at runtime without a function
pointer. There are many times you need to do that. The example of
qsort that another poster gave is a good one. Dynamic shared objects
would have no value at all without function pointers.

A call to a function has to be resolved at compile time. A call to a
function through a pointer is resolved at runtime.

If you're asking why a call to a function would need to be resolved at
runtime, re-read the answers you've already got.

 Fri, 19 Aug 2005 08:06:15 GMT   
   [ 17 post ]  Go to page: [1] [2]

Similar Threads

1. Browser calls CGI C function which sets an env var and call a c function crashes

2. Function pointer to member functions

3. About function that returns function pointer

4. Main function calling another main function

5. C functions calling C++ functions on Solaris

6. Tree of Functions calling Functions etc.

7. Call function within a function

8. Non-__init functions calling __init functions

9. function, function...who's got the function?

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software