#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    49
    Rep Power
    12

    Help with function pointers...


    Hello.

    My book touches on function pointers for about two paragraphs, and wasn't enough for me to grasp it. I ran across this page:
    http://www.function-pointer.org/fpt.html
    which did a much better job.

    I'm using the sample code (more or less) from their page, but I've got one problem. On the last line, you can see I'm passing 2 and 5 as floats to the function (which should run 2-5 = -3. However, it looks when they make it to the function, they're becoming 36.8144 and 2.01856 respectively.

    I assume I'm overlooking something foolishly, but can't for the life of me find it.

    NOTE: I cut out the "PLUS" function to make the example code smaller.

    Also, the only warnings I get are "control reaches end of non-void function" for the function pointer, and for the GetPtr2 function. Not quite sure what the warning means.

    I'm compiling with G++ via KDEV.
    TIA

    Code:
    #include <iostream>
    using namespace std;
    
    float Minus(float, float)
    {
      float a,b;
      cout << "a " << a << " b " << b << endl;
      return a-b;
    }
    
    float (*GetPtr1(char opCode))(float, float)
    {
      if(opCode == '+') return &Plus;
      if(opCode == '-') return &Minus;
    } 
    
    // Typedef Solution:
    typedef float(*pt2Func)(float, float);
    
    pt2Func GetPtr2(char opCode)
    {
      if(opCode == '+') return &Plus;
      if(opCode == '-') return &Minus;
    }
    
    int main()
    {
      cout << endl << "Executing 'Return_A_Function_Pointer'" << endl;
      float (*pt2Function)(float, float);
    
      pt2Function=GetPtr2('-');
      cout << pt2Function(2, 5) << endl;
    }
  2. #2
  3. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12

    Re: Help with function pointers...


    The "control reaches end of non-void function" for the function pointer means that not all control paths lead to a return() call. What happens if opCode is neither '+' or '-'? What are you going to return? It does not return any value for such a case.

    Also, your Minus function is improper. It has two local variables a and b. It initializes neither, then prints out their junk values, and then subtracts one from the other and returns the value. It makes no use of the parameters passed in. The proper format is as follows, in my revision of your program:

    Code:
    #include <iostream>
    using namespace std;
    
    float Plus(float a, float b)
    {
    	cout << "a " << a << " b " << b << endl;
    	return(a+b);
    }
    
    float Minus(float a, float b)
    {
    	cout << "a " << a << " b " << b << endl;
    	return(a-b);
    }
    
    float (*GetPtr1(char opCode))(float, float)
    {
    	if(opCode == '+') return &Plus;
    	if(opCode == '-') return &Minus;
    	return(NULL);
    } 
    
    // Typedef Solution:
    typedef float(*pt2Func)(float, float);
    
    pt2Func GetPtr2(char opCode)
    {
    	if(opCode == '+') return &Plus;
    	if(opCode == '-') return &Minus;
    	return(NULL);
    }
    
    int main()
    {
    	cout << endl << "Executing 'Return_A_Function_Pointer'" << endl;
    	float (*pt2Function)(float, float);
    
    	pt2Function=GetPtr2('-');
    	cout << pt2Function(2, 5) << endl;
    
    	return(0);
    }
    The pointer to the function should be checked to make sure it is not NULL before it is used, however. I also made main() return a value, as well, since it is a function.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    49
    Rep Power
    12
    Yeup -- that's it. Thanks!
    I suppose a little carelessness on my part.
    I guess I was just caught up in the trickiness of the function pointer thing that I forgot the basics.

    Cheers!

IMN logo majestic logo threadwatch logo seochat tools logo