on the fringe of C syntax/semantics

Tim_CDC_Roberts at cup.portal.com Tim_CDC_Roberts at cup.portal.com
Fri Oct 6 04:25:25 AEST 1989


Re:  How do I write a cast to a function pointer etc.?

Within the past few months, one of the gurus here posted a quick rule for
figuring out how to write a cast, which I made myself read over and over
until I memorized it.

To make a cast for a given simple or complex type, first write a declaration
of some "thing" of that type:

   int * (* func)();     func is ptr to function returning ptr to int

Then, remove the name of the "thing" and surround the declaration with
parentheses.  This becomes the cast:

   (int * (*)()) x       x is cast to ptr to function returning ptr to int

This method is so simple and elegant (and not intuitively obvious to a
beginning-to-intermediate C programmer), that it deserves to be in that
list of "Commonly asked C Questions", whenever someone gets around to
putting it together.

Tim_CDC_Roberts at cup.portal.com                | Control Data...
...!sun!portal!cup.portal.com!tim_cdc_roberts |   ...or it will control you.



More information about the Comp.lang.c mailing list