what are the applications of Function Pointers?

This is a discussion on what are the applications of Function Pointers? within the C Programming forums, part of the General Programming Boards category; Could anyone explain the areas, where the Function Pointers are applied ? Thanks, rssrik...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    6

    what are the applications of Function Pointers?

    Could anyone explain the areas, where the Function Pointers are applied ?

    Thanks,
    rssrik

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    Look at the prototypes for qsort() and bsearch() in stdlib.h

    They're also commonly used to implement callback functions in GUIs.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Dynamically call functions, where you don't know which functions to call. Also, provide an effective means to allow others to replace functions.

    For example, when a C program, when you return from main(), a function called exit() from stdlib.h is automatically called. This function cycles through some cleanup functions. You can register your own function to be called during this by giving a pointer to your particular function.

    Also, in Windows programming, GUI objects all have a function pointer associated with them that handle events. You can give your own function pointer to have your own custom handling of these same events.

    This allows you to "fake" OOP in C, which, in reality, is how C++ really handles OO concepts under the hood.

  4. #4
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    *fopen() used in file I/O
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    fopen() does not use function pointers (unless you're referring to some implementation or specific internal detail). It returns a pointer to an instance of a FILE struct.

    A function pointer is a pointer that points to a function.

  6. #6
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    I just got out of bed...
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    6
    understood somewhat !
    to understand more, I request someone to post a code or link to illustrate the usage of function pointers !!

    Regards,
    rssrik

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    Search the forums for examples of use of qsort().
    I've posted such examples many times already.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  2. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. function pointers and member functions
    By thor in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2002, 04:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21