Array of pointers to functions

Eugene D. Brooks III brooks at lll-crg.ARPA
Mon Apr 22 08:45:22 AEST 1985


Declaring arrays of pointers to functions returning pointers to what ever
is always a pain to get straight.  The slickest solution (other than having
godlike mental powers) is documented in "C Notes" by Zahn.  You use a set of
typedefs to get the job done a step at a time.

/* First the type that the function returns. */

typedef int *PI;	/* PI is a pointer to an int. */

/* Now the function type. */

typedef PI FPI();	/* FPI is a function returning a pointer to an int. */

/* Now pointer to the above function. */

typedef FPI *PFPI;	/* PFPI is a pointer to a function returning a pointer to an int. */

Now an array of these pointers is as for any array.

PFPI array[10];		/* An array of function pointers of size 10. */

By building up these rather complicated declarations using typedefs the logic
of what you are declaring and how to use it is it bit easier to understand.

See "C Notes" by Zahn for a longer explanation of the above and the corrections
to any possible errors in the preceeding description.



More information about the Comp.lang.c mailing list