how to properly use a function pointer to a particular function
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: how to properly use a function pointer to a particular function

  1. #1
    Join Date
    Apr 2013
    Posts
    6

    how to properly use a function pointer to a particular function

    #include <iostream>

    using namespace std;

    int function(int a,int b)
    {
    return a + b;
    }

    bool function2(int a,int b)
    {
    return a < b;
    }

    void function3(int c, int(*cmp)(int a,int b) )
    {
    bool (*compare)(int,int) = &function2;

    if( (*compare)(c,(*cmp)(a,b)))
    {
    cout << " this number is way bigger then the two " <<endl;
    }else{
    cout << " this number is lower " <<endl;
    }
    }

    int main(int argc,char *argv[])
    {
    int (*foo)(int,int) = &function;

    int a = 5, b = 10;
    int sum = (*foo)(a,b);

    cout << " the sum of two values: " << sum <<endl;

    bool (*foo2)(int,int) = &function2;

    if((*foo2)(a,b))
    {
    cout << " this is the power of c " <<endl;
    }else {
    cout << " this is not the power of c " <<endl;
    }

    system("pause");
    return EXIT_SUCCESS;
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,196

    Re: how to properly use a function pointer to a particular function

    Is there a question implied here, or are you just presenting this as an example?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 2013
    Posts
    6

    Re: how to properly use a function pointer to a particular function

    its a question

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    1,997

    Re: how to properly use a function pointer to a particular function

    When you post code, please format and indent it properly first and use code tags (Go Advanced, select code and click '#') so that the code is readable. Also, it would be useful if you actually asked a specific question!

    As you haven't asked a specific question, I'm assuming you are referring to the compiler error regarding undeclared identifiers in the line

    Code:
    if ((compare)(c, (*cmp)(a, b))) {
    This is because a and b haven't been declared!

    Looking at the function definiton, you have
    Code:
    void function3(int c, int(*cmp)(int a,int b) )
    This function takes 2 parameters. The first is an int, the second is a function that takes as arguments 2 ints and returns an int. The names of these arguments is ignored here by the compiler (you don't even need to provide them - the same way as you don't need to provide argument names in function declarations). If you want to pass two ints to function3 as well then the function definition would be

    Code:
    void function3(int c, int(*cmp)(int, int), int a, int b)
    When dealing with function pointers, I like to use typedefs to define a function type.

    Code:
    typedef int (*IFUNCII)(int, int);
    Which defines a type IFUNCII as a function pointer for functions that take 2 int parameters and return an int. Using this, the function3 definition would be

    Code:
    void function3(int c, IFUNCII cmp, int a, int b)
    An example of using it is

    Code:
    function3(12, function, 5, 10);
    I hope this helps.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Tags for this Thread

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center