Thread: Function returning function pointer : help

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    8

    Function returning function pointer : help

    I wrote following program,
    Its compiling without errors but giving wrong output.
    output should be 2 but giving 0;
    Can anybody tell me what is my mistake?
    Thanx in avd.

    Code:
    float Plus    (float a, float b) { return a+b; }
    float Minus   (float a, float b) { return a-b; }
    /*
     * Solution using a typedef: 
     * Define a pointer to a function which is taking
     * two floats and returns a float
     */
    typedef float(*Op_fptr)(float, float);
    /*
     * Function takes a char and returns a function pointer. <opCode> specifies which function to return
     */
    Op_fptr GetPtr2(const char opCode)
    {
       if(opCode == '+')
          return &Plus;
       else
          return &Minus; // default if invalid operator was passed
    }
    int main()
    {
       /* float (*pt2Function)(float, float) =  NULL; */
       Op_fptr pt2Function;
       pt2Function=GetPtr2('-');   // get function pointer from function 'GetPtr2'
       printf("minus %d\n",(*pt2Function)(4, 2));   // call function using the pointer
      return 0;
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your problem is that in printf you're using %d where you should be using %f. Turning up the warning level on your compiler should tell you that.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    8

    Thanx

    Quote Originally Posted by oogabooga View Post
    Your problem is that in printf you're using %d where you should be using %f. Turning up the warning level on your compiler should tell you that.
    Thanx oogabooga,
    Its solved and with option -Wall its showing that warning too.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    8
    Hi,

    again some problem,

    In above program if I add scanf then its giving following warnings with -Wall options.

    temp1.c:24: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
    temp1.c:24: warning: format ‘%d’ expects type ‘int *’, but argument 3 has type ‘int’
    temp1.c:24: warning: format ‘%c’ expects type ‘char *’, but argument 4 has type ‘int’

    if I run the program then it ending in segmentation fault.

    TIA.

    Code:
      1 #include<stdio.h>
      2 
      3 float Plus    (float a, float b) { return a+b; }
      4 float Minus   (float a, float b) { return a-b; }
      5 /*
      6  * Solution using a typedef: 
      7  * Define a pointer to a function which is taking
      8  * two floats and returns a float
      9  */
     10 typedef float(*Op_fptr)(float, float);
     11 Op_fptr GetPtr2(const char opCode)
     12 {
     13    if(opCode == '+')
     14       return &Plus;
     15    else
     16       return &Minus;
     17 }
     18 int main()
     19 {
     20    int a=0,b=0;
     21    char c = '+';
     22    //float (*pt2Function)(float, float) =  NULL;
     23    Op_fptr pt2Function;
     24    scanf("%d %d %c",a,b,c);
     25    pt2Function=GetPtr2(c);   // get function pointer from function 'GetPtr2'
     26    printf("minus %f\n",(*pt2Function)(a, b));   // call function using the pointer
     27   return 0;
     28 }

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    8
    If I used %f in scanf then also it is giving similar warnings,

    temp1.c:24: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘int’
    temp1.c:24: warning: format ‘%f’ expects type ‘float *’, but argument 3 has type ‘int’
    temp1.c:24: warning: format ‘%c’ expects type ‘char *’, but argument 4 has type ‘int’

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This:
    Code:
    scanf("%d %d %c",a,b,c);
    should be:
    Code:
    scanf("%d %d %c", &a, &b, &c);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning a function pointer from a function
    By StainedBlue in forum C++ Programming
    Replies: 4
    Last Post: 11-01-2010, 10:26 PM
  2. Function returning pointer to function
    By DL1 in forum C++ Programming
    Replies: 6
    Last Post: 08-07-2009, 10:27 AM
  3. Returning pointer to function from function
    By Bargi in forum C Programming
    Replies: 7
    Last Post: 06-09-2008, 01:43 AM
  4. Returning pointer from function question
    By Rune Hunter in forum C++ Programming
    Replies: 12
    Last Post: 07-12-2007, 09:45 AM
  5. function returning pointer
    By blue_gene in forum C Programming
    Replies: 7
    Last Post: 04-19-2004, 02:35 PM

Tags for this Thread