function pointer tutorial may be helpful..

Discussion in 'C Programming' started by devesh, Mar 5, 2008.

  1. devesh

    devesh Guest

    function pointer is a variable which points to the address
    of a function.
    it is basically used for removing/replacing switch statement and if
    else statement.
    it is very usefull to implement callback function.callback functions
    are mostely
    used in driver writing.it can also be used insted of virtual function
    to save some time...


    now consider this example to understand how function pointer works...


    #include<stdio.h>
    static int my_function(int a)
    {

    printf("my_function :%d",a);
    return(2*a+3);
    }
    int main(void)
    {
    int (*new_function)(int)= my_function; /* here a function
    new_function is decleared, the address of the function named
    my_function is assigned to it, and function is invoked by
    dereferencing the pointer.*/
    int x;
    x=(*new_function)(10);
    return 0;
    }


    now consider another example..

    void young(int);
    void old(int);

    int main(void) {
    void (*fp)(int);
    int age;
    printf("How old are you? ");
    scanf("%d", &age);
    fp = (age > 30) ? old : young;
    fp(age);
    return 0;
    }
    void young(int n) {
    printf("Being only %d, you sure are young.\n", n);
    }
    void old(int m) {
    printf("Being already %d, you sure are old.\n", m);
    }
    here the function pointer *fp is declared here as void (*fp)(int),
    which specifies both the return type (void) and the types of arguments
    (int) of the function. We then assign the pointer to a particular
    function, and having done so, can then call the function just as we
    normally would.


    Another example of the function pointer is..

    void young(int);
    void old(int);
    void greeting(void (*)(int), int); // function pointer is used to call
    the function old/young
    int main(void) {
    int age;
    printf("How old are you? ");
    scanf("%d", &age);
    if (age > 30) {
    greeting(old, age);
    }
    else {
    greeting(young, age);
    }
    return 0;
    }
    void greeting(void (*fp)(int), int k) {
    fp(k);
    }
    void young(int n) {
    printf("Being only %d, you sure are young.\n", n);
    }
    void old(int m) {
    printf("Being already %d, you sure are old.\n", m);


    now come to the million dollar question
    I have function like arithmetic( int1, int2, int3)
    Int3: Arg for select the operation 1. Add 2. Mul 3.
    Div 4. Sub
    Write the program for above condition without using
    switch and if-else

    Void arithematic(float a,float b,float (*operation_arithematic)
    (float,float));
    {
    float result=operation_arithematic(a,b);
    printf("the result is = %f",result);
    }
    float plus(float a,float b)
    {
    return(a+b);
    }
    float minus(float a,float b)
    {
    return(a-b);
    }
    float multiply(float a,float b)
    {
    return(a*b);
    }
    float divide(float a,float b)
    {
    return(a/b);
    }
    void main( )
    {
    arithematic(3,5,&plus);
    printf("%f",result);

    arithematic(7,5,&minus);
    printf("%f",result);

    arithematic(7,5,&multiply);
    printf("%f",result);

    arithematic(7,5,&divide);
    printf("%f",result);

    }

    I think this will work fine And this will be very useful to
    understand .
    devesh, Mar 5, 2008
    #1
    1. Advertising

  2. devesh said:

    > function pointer is a variable which points to the address
    > of a function.


    A function pointer is a value that is the address of a function. A function
    pointer is said to "point to a function" rather than "point to the address
    of a function".

    > it is basically used for removing/replacing switch statement and if
    > else statement.


    That is one possible use.

    > it is very usefull to implement callback function.callback functions
    > are mostely
    > used in driver writing.


    Actually, they are used much more widely than that.

    > it can also be used insted of virtual function
    > to save some time...


    C doesn't have virtual functions.

    > now consider this example to understand how function pointer works...
    >
    >
    > #include<stdio.h>
    > static int my_function(int a)
    > {
    >
    > printf("my_function :%d",a);
    > return(2*a+3);
    > }
    > int main(void)
    > {
    > int (*new_function)(int)= my_function; /* here a function
    > new_function is decleared,


    No. There is no function by that name; new_function is not a function, but
    a function pointer.

    > the address of the function named
    > my_function is assigned to it, and function is invoked by
    > dereferencing the pointer.*/


    Right.

    <snip>

    > now consider another example..
    >
    > void young(int);
    > void old(int);
    >
    > int main(void) {
    > void (*fp)(int);
    > int age;
    > printf("How old are you? ");


    When you use any variadic function (and that certainly includes printf),
    you need to provide a valid function prototype for it. You can do this by
    adding:

    #include <stdio.h>

    at the top of your code. If you don't provide such a prototype, the
    behaviour of your program is undefined. (The same applies to scanf and
    other variadic functions.)

    > scanf("%d", &age);


    Check that this scanf call succeeded. The scanf function returns a useful
    value that you should test to ensure that it's what you expect.

    <snip>

    > Void arithematic(float a,float b,float (*operation_arithematic)
    > (float,float));


    C has no Void type. If you don't define it yourself, it doesn't exist.
    (Hint: C is case-sensitive.)

    <snip>

    > void main( )


    In C, main returns int.

    <snip>

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Mar 5, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sam Iam
    Replies:
    0
    Views:
    435
    Sam Iam
    Jan 31, 2004
  2. Rahmi Acar
    Replies:
    5
    Views:
    410
    Karl Heinz Buchegger
    Jul 28, 2003
  3. Aahz
    Replies:
    0
    Views:
    375
  4. kingchuffalo
    Replies:
    0
    Views:
    857
    kingchuffalo
    Sep 21, 2008
  5. Replies:
    5
    Views:
    334
Loading...

Share This Page