Thread: defining a function pointer which recieves a function pointer

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    63

    defining a function pointer which recieves a function pointer

    Hi, maybe i can clarify what im trying to do a little bit. I want to make a program with a modular design, and the way ive decided to do it is to have one central "request" function which is able to get the address of any function in any .dll which is loaded. However, for a .dll to get the address of this function, the "core" must load the .dll and call "initialize" which receives the function pointer to the "request". The problem is I cant find any example of a function pointer defined which receives a function pointer as an argument.

    Code:
    int (*initialize)(what goes in here) = NULL;  //what is the type of a function pointer??? thats what it all boils down to.
    &initialize = GetProcAddress(hInstLibrary, "initialize");

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Just use a couple of typedef's, it makes life so much easier

    typedef int (*fooFn)( int );
    typedef int (*barFn)( fooFn );


    Then
    barFn init = 0;
    init = GetProcAddress(hInstLibrary, "initialize");
    init( aFooFn );
    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.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    &initialize = GetProcAddress(hInstLibrary, "initialize");
    no & neccessary.

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    63
    ill use the type defs, but im still curious as to how to define a pointer.

    as for the &, its technically the "correct" way to do it (so i believe), and in my opinion not using short hand makes code much more readable.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I think you're thinking of this:
    Code:
    int *i, j;
    i = &j;

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    The & is wrong imo.... You're dealing with a pointer, which holds an address. There's no reason to try to store something in the address of a pointer.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by *DEAD* View Post
    ill use the type defs, but im still curious as to how to define a pointer.

    as for the &, its technically the "correct" way to do it (so i believe), and in my opinion not using short hand makes code much more readable.
    You can do either.

    Short hand would be calling a variable "x" instead of "numElephantsInRoom." Leaving out an unnecessary ampersand isn't exactly conducting major surgery. I fail to see how its presence or absence in this case makes code any more/less clear.

    I'd say 50% of the code I've seen uses it when taking function addresses, the other half doesn't.

    EDIT: Also, your pointer type is something like:

    Code:
    int (*func)(int (*)(int));
    Pointer to function which takes (pointer to function which takes int returning int) returning int.
    Last edited by brewbuck; 07-04-2007 at 10:02 AM.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    However, for member function pointers in C++, you need to use &function. I think that's where the myth comes from. For ordinary function pointers, in both languages, function and &function are equivalent.

    http://cboard.cprogramming.com/showthread.php?t=77504
    http://www.newty.de/fpt/fpt.html#assign
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Glib and file manipulation
    By unixOZ in forum Linux Programming
    Replies: 1
    Last Post: 03-22-2004, 09:39 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM