CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2003
    Location
    India
    Posts
    196

    Function Pointers...

    Hey gurus,

    I am completely confussed with the functionality of the Function Pointers.

    Can anyone help me out in knowing where exactly this function pointers are used.

    Here is the simple example which i have copied from one of the books.

    /****************************************************************************/
    #include <iostream>
    using namespace std;
    void func()
    {
    cout << "func() called..." << endl;
    }

    int main()
    {
    void (*fp)(); // Define a function pointer

    fp = func; // Initialize it

    printf("value in *fp after initialize is %d\n",(*fp));
    printf("after initialize fp is %d\n",fp);

    (*fp)(); // Dereferencing calls the function

    void (*fp2)() = func; // Define and initialize
    (*fp2)();

    return 0;
    }
    /****************************************************************************/

    In this example what exactly is the need of the function pointer "fp". As we can directly call the function
    "func()".

    /*****************************************************************************/
    void (*fp)(); // Define a function pointer
    /*****************************************************************************/

    Is this the correct way to define. As i know that this is something called declaration
    but not definition.

    /*****************************************************************************/
    printf("value in *fp after initialize is %d\n",(*fp));
    printf("after initialize fp is %d\n",fp);
    /*****************************************************************************/

    what will be the output of the above statement.

    Thanks in advance.

    venky

  2. #2
    Join Date
    Sep 2002
    Posts
    1,747
    Function pointers are a way to pass around function instances when all you specify is the function type (the collection of return and parameter types). So you can tell a distant function somewhere that when it gets to a particular point you want it to call a particular function instance, and that distant function never needs to make any assumptions except the type. This is common in callback mechanisms.
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  3. #3
    Join Date
    Feb 2002
    Posts
    5,757
    In this example, fp is a type, not a variable.

    fp OneFunctionPointer = ...

    Function pointer is an essential solution in many situations. One example is the use of it as part of a function parameter. Another situation could be a replacement for many if/else and switch statements.

    Kuphryn

  4. #4
    Join Date
    Feb 2003
    Location
    Bilbao
    Posts
    513
    Basically you can use function pointers when you want to make some kind of dynamic process...

    For example... Imagine that you are developing a script interpreter... You read a text file and want to call a function with every key word in the script. (For example, READ, WRITE, OPEN, GOUP, GODOWN...)

    Of course you can make a giant switch with all the keywords, but I would prefer to do a linked list of structures that contain the keyword and the function pointer to handle it...
    Caronte
    Si tiene solución... ¿por qué te preocupas?
    Si no tiene solución... ¿por qué te preocupas?

  5. #5
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    Originally posted by kuphryn
    In this example, fp is a type, not a variable.

    fp OneFunctionPointer = ...
    I am quite positive that the original example for fp is a variable. Only when typedef is being used, fp becomes a type.

    Code:
    typedef void (*fp)();    // Declaring a type
    fp OneFunctionPointer = ...
    
    void (*fp1)();    // Declaring a variable
    fp1 = ...

  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    I'd take the book back if they can't come up with a better example than that.

    Try this:
    Code:
    #include <iostream>
    using namespace std;
    void func1()
    {
       cout << "func1() called..." << endl;
    }
    
    void func2()
    {
       cout << "func2() called..." << endl;
    }
    
    int main()
    {
        void (*fp)();
    
        fp = func1;
    
        printf("value in *fp after initialize is %d\n",(*fp));
        printf("after initialize fp is %d\n",fp);
    
        (*fp)();
    
        fp = func2;
    
        printf("value in *fp after initialize is %d\n",(*fp));
        printf("after initialize fp is %d\n",fp);
    
        (*fp)();
    
        return 0;
    }
    Note here how the same function pointer is used to call two different functions.

    Incidentally, when I said "take the book back": the use of printf() when it's already included <iostream> simply confirms that the author shouldn't be trusted to give good advice. And I don't see the purpose of the statement:

    printf("value in *fp after initialize is %d\n",(*fp));
    Last edited by Graham; January 8th, 2004 at 02:48 PM.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  7. #7
    Join Date
    Feb 2002
    Posts
    5,757
    Okay. I did not pay attention to the typedef. My mistake.

    Kuphryn

  8. #8
    Join Date
    Feb 2003
    Location
    Brazil
    Posts
    335

    qsort is a good exemple

    A good exemple of functions pointers use can be found on qsort and bsearch functions. On bsort for exemple, that sorts an array of elements defined by the user, all the work of sorting is made internally but qsort don´t know how compare two elements of this array. This funcionallity is provide by a user´s function and a pointer to this function is passed to qsort.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured