calling fuction with function pointer in C99

  • Thread starter Shivanand Kadwadkar
  • Start date
S

Shivanand Kadwadkar

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
 
L

lawrence.jones

Shivanand Kadwadkar said:
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.
 
B

BartC

Shivanand Kadwadkar said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top