![]() |
calling fuction with function pointer in C99
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 |
Re: calling fuction with function pointer in C99
Shivanand Kadwadkar <shivanand.kadwadkar@gmail.com> 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 |
Re: calling fuction with function pointer in C99
"Shivanand Kadwadkar" <shivanand.kadwadkar@gmail.com> wrote in message news:72dee84d-c64d-4489-8c62-c09d5ab9c4a4@35g2000prb.googlegroups.com... > 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 |
| All times are GMT. The time now is 07:44 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.