Thread: Function Pointers in C

  1. #1
    Registered User
    Join Date
    Aug 2004

    Function Pointers in C

    Can any one suggest the relative advantages of Function Pointers over directly calling the functions in C.
    In which circumstances we realise the full efficiency of Function Pointers.

    Thanx in Advance
    Gobinath Krishna

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    It's hard to tell, but first that cross my mind is something like this: (form Prelude's tutorial about Binary Serach Tree in the FAQ)
    Source 7:
    void pretree_levelorder (
      struct pretree_node *header,
      void (*action) ( struct pretree_node *node ) )
      struct pretree_node *save[50];
      int front = 0;
      int back = 0;
      if ( header == NULL )
      save[front++] = header;
      while ( front != back ) {
        header = save[back++];
        action ( header );
        if ( header->link[0] != NULL )
          save[front++] = header->link[0];
        if ( header->link[1] != NULL )
          save[front++] = header->link[1];
    As you can see function poninter can parameter in another function.
    It can be handy when you want to call same function with some date and but each time you need to perform some action on that data. For instance in one call action could be a simple printf to print data on the screen or in another call you can send another function through pointer that will perform some data processing.
    I don't know if this helps, but you can find out a lot using google

    I suggest you reading this:

  3. #3
    Registered User
    Join Date
    Mar 2005
    hi man.
    the advantage is the efficiency in access address in memory directly in functions....the method of indexation of vector is most waste that this, because the access with pointer work directly with aritmetmetic of which is based in aritmetic binary of pointers that is more fast.
    The pointer is useful whenever work with strings that in true strings are simply address consecutives location in memory.
    Working with pointers in begin can be bit hard...but is just in begin...after seens work better than begin.
    Last edited by Salem; 03-09-2005 at 12:34 PM. Reason: Edit profanity - read your PM and don't let me see this again!

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    ?????????????????????????????????????????????????? ?????????????????????????????????


    Anyways, the advantage to using function pointers has nothing to do with efficiency. Whether it is a true function or a pointer to a function both methods will use an address in memory. The advantage is that you can link function pointers to functions - so you can unlink them just as easily. For instance let's say you have a function pointer called MathOperatoion.

    void (*MathOperation)(int operand1,int operand2,int *out);

    Now you can create arithmetic functions.

    void Add(int v1,int v2,int *out)
    void Subtract(int v1,int v2,int *out)
    void Multiply(int v1,int v2,int *out)
    void Divide(int v1,int v2,int *out)
    The advantage here is we can link our function pointer to any of these functions.

    int v1,v2,v3;
    This will call Add since we linked MathOperation to Add. The same can be done for all 4 functions.

    This is a stupid example and my pointers might be a bit off, but you get the idea.

  5. #5
    Registered User joed's Avatar
    Join Date
    Mar 2004
    A function pointer can simplify handling several versions of a routine. As another example, you might have some pixel blending functions for sprites (just the prototypes):
    int blend_normal(int, int, int);
    int blend_add(int, int, int);
    int blend_subtract(int, int, int);
    Then a global pointer is defined:
    int (*blend)(int, int, int);
    To select between functions, the pointer is simply redirected to the appropriate function:
    blend = blend_normal;
    blend = blend_add;
    So instead of having separate sprite-drawing routines for normal, add, and subtract, the sprite function would just use the global pointer for blending operations. The sprite function wouldn't know or care which blender is in use. Note that this doesn't improve performance, and it's a bit unstructured, but it might be space-efficient.

    The other catch is that i'm not sure what happens if you try and point to an function defined as inline. It appears to work, so I assume the compiler keeps normal, non-inlined copies?

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Oregon, USA
    It's also really useful for "plugins". I incorporated a plugin system to a MUD-ish server called TinyMAZE. Here's the code if you want to check it out:
    If you understand what you're doing, you're not learning anything.

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    The only catch to function pointers is in C++ - you CANNOT have a function pointer in a class point to a function in another class.

    Each function member in a class has an implicit _thiscall call type. This cannot be casted away and cannot be type-casted to another type like _stdcall or _pascal. I think the reasoning behind this is that you could override the inherent C++ protection mechanisms by using function pointers. So to avoid the abuse, I think they placed _thiscall there - also so that the compiler knows this function must be called from an object of that type.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  2. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  3. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. function pointers and member functions
    By thor in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2002, 04:22 PM