Mail Archives: djgpp/1997/11/29/12:42:22
Vic <tudor AT cam DOT org> wrote:
> If I say
> but[number].(*action)(); I get a parse error before "("
> but if I say
> void (*thing)();
> thing=but[number].action;
> (*thing) ();
> it works and calls the right function. Why?
Because saying structure.(*fieldname) is not valid C
syntax. It doesn't make sense. If it were to make any sense
at all then that expression would mean something like:
take the contents of the variable 'action' and use them
to determine which field in 'structure' to access
C doesn't support that.
The thing which you need to dereference is the expression
but[number].action
(which you do when you assign it to 'thing' first). So
you need to call it as
(*but[number].action)();
In practice, you don't need to because pointers to functions
are automagically dereferenced when called, so you can say
but[number].action();
quite happily.
BTW, this is off-topic for this newsgroup, questions about C
syntax should be asked in one of the C newsgroups. Or ask
your teacher. Or read a C primer (Kernighan and Richie is
pretty good, and has sections about pointers, functions and
structures).
Chris C
- Raw text -