Thread: pointers to functions

  1. #1
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105

    pointers to functions

    Actually i am new to this pointer to functions and not able to find out what is the wrong in using the pointer to functions. The code i tried was this:
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    #define PI 3.1415926
    double y(double);
    double cos(double);
    void table(double (*f)(),double,double,double);
    
    int main()
    {
         printf("TABLE OF y(x)= 2*x*x-x+1\n\n");
         table(y,0.0,2.0,0.5);
         printf("TABLE OF cos(x)\n\n");
         table(cos,0.0,PI,0.5);
         getch();
         return 0;
    }
    void table(double (*f)(),double min,double max,double step)
    {
         double a,value;
         for(a=min;a<=max;a+=step)
            {
                value=(*f)();
                printf("%5.2f    %10.4f\n",a,value);
            }
         return;
    }
    double y(double x)
    {
         return(2*x*x-x+1);
    }
    I tried in codeblocks compiler but was not able to correct although in turbo C++ it runs by some changes in the code,but not in codeblocks.Please view into it.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The functions being used have the wrong signature (which implies that you may need to rethink why you are doing things the way you are). To compile, they would need to take no arguments and return a double. Also, when you call a pointer to a function, you can invoke it just as you would in a normal function call (eg: no need to dereference it). Another simplification is to use typedef the function pointer (saves a bit of typing, anyway):

    Code:
    
    
    typedef void ( * fp_type )( int, double );
    
    void f( fp_type fp, int i, double d )
    {
          fp( i, d );
    }
    Last edited by Sebastiani; 01-12-2010 at 10:57 AM. Reason: spleling

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Sebastiani View Post
    The functions being used have the wrong signature (which implies that you may need to rethink why you are doing things the way you are). To compile, they would need to take no arguments and return a double.
    Not quite true. That would be double (*f)(void). rakeshkool's pointer "double (f*)()" will take any number and form of argument, so it is okay. Altho "double (f*)(double)" would maybe be more optimal and at least makes the code more comprehensible, etc.

    But you should call it like this:
    Code:
    value=f(a);
    Last edited by MK27; 01-12-2010 at 11:08 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by MK27 View Post
    Not quite true. That would be double (*f)(void). rakeshkool's pointer "double (f*)()" will take any number and form of argument, so it is okay. Altho "double (f*)(double)" would maybe be more optimal and at least makes the code more comprehensible, etc.

    But you should call it like this:
    Code:
    value=f(a);
    Ah, right. Yes, that's one feature of C I would *never* think of using! But yes, the real problem is the fact that the function is being invoked without arguments, as you pointed out...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  2. HELP WITH FUNCTIONS and POINTERS!!!
    By cjohnson412 in forum C++ Programming
    Replies: 4
    Last Post: 08-11-2008, 10:48 PM
  3. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  4. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM