calling fuction with function pointer in C99

Discussion in 'C Programming' started by Shivanand Kadwadkar, Jan 1, 2011.

  1. C99 says following are vallid function call with function pointer

    1. (&f)();
    2. f();
    3 (*f)();
    4. (**f)();
    5. (***f)();
    6. pf();
    7. (*pf)();
    8. (**pf)();
    9. (***pf)();

    here my question with respect to normal pointer.

    in normal pointer *,**,& has different meaning but with function
    pointer it look like those dont have any special meaning(all will do
    the same operation).

    Is *,** and & dont have any special meaning with function pointer ?
    Why they are not making any difference in calling function ?

    Thanks all for your comments
    Shivanand Kadwadkar, Jan 1, 2011
    #1
    1. Advertising

  2. Shivanand Kadwadkar

    Guest

    Shivanand Kadwadkar <> wrote:
    >
    > in normal pointer *,**,& has different meaning but with function
    > pointer it look like those dont have any special meaning(all will do
    > the same operation).


    Looks are deceiving. Those operators all have their normal meanings,
    but the C language says that whenever an expression with a function type
    is evaluated in a context that requires a value, the expression is
    automatically converted into a pointer to the function. So, in an
    expression like (***f)(), f has function type and is evaulated in
    context that requires a value, so it is automatically converted into a
    pointer to the function. The first * then turns it back into a
    function, but it's again in a context that requires a value so it is
    automatically converted back into a pointer again and so on for the rest
    of the * operators.
    --
    Larry Jones

    These findings suggest a logical course of action. -- Calvin
    , Jan 1, 2011
    #2
    1. Advertising

  3. Shivanand Kadwadkar

    BartC Guest

    "Shivanand Kadwadkar" <> wrote in message
    news:...
    > C99 says following are vallid function call with function pointer
    >
    > 1. (&f)();
    > 2. f();
    > 3 (*f)();
    > 4. (**f)();
    > 5. (***f)();
    > 6. pf();
    > 7. (*pf)();
    > 8. (**pf)();
    > 9. (***pf)();
    >
    > here my question with respect to normal pointer.
    >
    > in normal pointer *,**,& has different meaning but with function
    > pointer it look like those dont have any special meaning(all will do
    > the same operation).
    >
    > Is *,** and & dont have any special meaning with function pointer ?
    > Why they are not making any difference in calling function ?


    The '()' operator requires 'ptr to function' on it's left side. An ordinary
    function name is converted by C into a 'ptr to function', by putting an
    implied & in front of it, as in your example (2). (Which saves having to
    stick & in front of 99% of C function calls.)

    The other examples may or may not already be ptr to functions, ptr to ptr to
    function, etc, as C is weird about not caring about extra "*" here:

    #include <stdio.h>
    #include <stdlib.h>

    void testfn(int n){
    printf("Testfn: %d\n",n);
    }

    int main(void){
    void (*fn1)(int)=&testfn;
    void (**fn2)(int)=&fn1;
    void (***fn3)(int)=&fn2;

    testfn(1);
    (&testfn)(2);
    (***testfn)(3);

    (fn1)(10);
    (******fn1)(11);

    (*fn2)(20);
    (*******************fn2)(21);

    (**fn3)(30);
    }

    Look at the calls to testfn(), an ordinary function; I've put an extra "***"
    there, and it doesn't mind.

    fn1 is an actual ptr to function, so that's OK as it is (and again extra ***
    can be put in).

    fn2 is a ptr to ptr to function, and it needs at least one "*" to make it a
    ptr to function.

    While fn3 is a ptr to ptr to ptr to function, and needs at least two "*" to
    turn it into ptr to ptr to function.

    I've no idea why these extra "*" dereference ops are tolerated. It is just
    misleading when looking at code.

    --
    Bartc
    BartC, Jan 1, 2011
    #3
    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. mike
    Replies:
    3
    Views:
    362
    Chris Botha
    Nov 16, 2005
  2. somez72
    Replies:
    4
    Views:
    4,343
    Roedy Green
    May 3, 2004
  3. MiniDisc_2k2
    Replies:
    2
    Views:
    382
    MiniDisc_2k2
    Jul 3, 2003
  4. Replies:
    3
    Views:
    3,633
    Chris Torek
    Feb 20, 2006
  5. Replies:
    3
    Views:
    575
    Keith Thompson
    Mar 31, 2007
Loading...

Share This Page