Thread: Pointer to Function(names)

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

    Pointer to Function(names)

    Hi,

    in my c++-book is an example how to use pointer to functions:

    Code:
    compare(double, double);
    (funcptr*)(double, double);
    
    funcptr = compare;
    (funcptr*)(double, double);
    this would call compare. (I don't have the book here, so i am not 100% sure if the * belongs in front or at the end of funcptr.

    My problem is now in my case:
    Code:
    string funcname = "compare";
    funcptr = funcname;
    Of course i can't just write it the same like above, since one is a pointer and one is a string object.
    Anyone could help me, how i could make it work?


    Thanks

    Hork83


    PS: Why I need this? i'd like to write less code due to the large if/case i would need to write for any possible option.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You got all of your function pointer stuff confused, so you might want to review it all.

    As far as the names go, if you're using a .dll, you obtain a function pointer to the function in the DLL by means of GetProcAddress(), I think, which takes a C string. I've heard of cases of doing this on the .exe itself, but I don't think it's officially supported, so your milage may vary.

    In terms of *nix dynamic libraries, I believe there is a similar way to go about it, but I don't know the specific way to do it.

    Otherwise, you could build your own table of function names and pointers in a function, and then obtain the right function pointer from that function, given the name.

    Code:
    somefuncptr getFunction(string s)
    {
            if(s == "bleh")
                    return bleh_ptr;
            ....
    }

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The "syntax" for function pointer declaration is
    Code:
    returnType (*funcptr)(parameter(s));
    
    e.g.
    
    double (*compptr)(double, double);
    The parenthesis is necessary to differentiate between pointer to a type and function pointers - it ties the pointer to the parenthesis after, rather than a function that returns a pointer to a "returnType".

    What I'd like to understand is what you actually want to achieve with your string to function pointer conversion. But perhaps this is a way:

    Code:
    struct {
       char *str;
       int (*funcptr)(double, double);
    } table[] = { { "compare", compare },
                         { "add", add },
                         { "sub", sub } };
    Then just loop through table and compare the string with the "str" in the struct. Or if you need to do it "more clever", you build a hash-table or a sorted list so that you can search more quickly - assuming there is enough table entries and searches to require this.

    --
    Mats

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Note that you can assign pointers with two syntaxes:
    Code:
    int (*funcptr)(double, double) = compare;
    int (*funcptr)(double, double) = &compare;
    You can also call a function pointer with two syntaxes:
    Code:
    (*funcptr)(x, y);
    funcptr(x, y);
    There are arguments for both, each of which are valid, which is why they both exist. I don't feel like typing them out here; search the board or the net if you're interested.

    I prefer assigning with compare and calling with (*funcptr) myself, but everyone has their own programming style.
    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. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  2. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM