
Simple question about Pointers to Functions
Quote:
> > 5: void (*ptr)();
> > 6: void test();
> > 7:
> > > 8: ptr = test;
> > 9: ptr();
> > 10: (*ptr)();
> >both calls on line 9 and 10 work, but I am not quite sure I understand
> >why the call ptr() works. (correct way to call is on line 10). Can
> >someone be kind as to shed light on the subject ????
> ANSI C has allowed you to call the function through the pointer using
> normal function calling convention. Without that allowance, line 9
> would probably fail. (ie, it doesn't really make sense, but it's
> convenient, so they _defined_ lines 9 and 10 to mean the same thing.)
Um I must disagree, we dont have any problems with arrays and pointers to arrays
being handled differently, therefore line 9 is preferable. The () have the
same significance for functions as [] for arrays. The point is that there can be only
1 meaning so addign a * or an & is irelevant. My preffered route is:-
typedef void Func_t(void);
Func_t test; /* forward decleration of test */
Func_t *Func; /* decleration of pointer to a function of type:- void name(void) */
Func = test;
Func(); /* calls the function test */
You can say Func = &test; for the assignement and you can say (*Func)() to call the
function. But then the syntax looks mighty awfull.
--
=============================================================================
Fax: +44 (0)31-667 7209
=============== So long and thanks for all the fish =========================