Thread: function pointer calculator

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    110

    function pointer calculator

    Hi, I'm trying to learn about function pointers and how to use them.

    I have the following partially written program in c for a simple calculator, and without using a switch statement, how can I complete it using function pointers?

    Code:
    //declare functions
    
    float plus(float a, float b) { return a+b; }
    float minus(float a, float b) { return a-b; }
    float times(float a, float b) { return a*b; }
    float divide(float a, float b) { return a/b; }
    
    int main() {
    
    //declare function pointers
    
      float(*fptr_p)(float, float);
      float(*fptr_m)(float, float);
      float(*fptr_t)(float, float);
      float(*fptr_d)(float, float);
    
    //point ptr's to functions
    
      fptr_p=+
      fptr_m=−
      fptr_t=×
      fptr_d=÷
    
    return 0;
    }
    usually I would get the input from the command line here, and use a switch statement. This is probably the easiest way of doing things, and function pointers need not be used, but I'm sure there is a way of using them for this, and I can't think how. I'm just about familiar withn how to pass a function pointer as an argument of a function and how to return a function pointer from a function, but I can't see how this gets rid of the need for a switch statement.
    PHI is one 'H' of alot more interesting than PI!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You still need some way to translate from '+' to the function for addition. You could do this by having a translation from + to an enumerated value, eg. OP_PLUS, and then use OP_PLUS as an index into an array of function pointers.

    Or you could make an array of 128 elements, and let only those that correspond to '+', '-', '/', '*' be set to non-NULL value, and then use the operator character as an index into the array.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Thanks, I'll give it a try.
    PHI is one 'H' of alot more interesting than PI!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    By the way, would it not make more sense to declare ONE function pointer typedef for all 2-operand operators - after all, the operators all take the same two operands in the same order and way.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Replies: 7
    Last Post: 07-04-2007, 12:46 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Glib and file manipulation
    By unixOZ in forum Linux Programming
    Replies: 1
    Last Post: 03-22-2004, 09:39 PM