void * = pointer to a function

Patrick Martin pm0 at reef.cis.ufl.edu
Sun Mar 10 19:26:53 AEST 1991


I am trying to get a grip on pointers to functions.  Here is
the code I am having a problem with (Simplified to the bare
essense of the problem)

#include <stdio.h>

int Output_Num(int a)
{
   printf("The Number is: %d",a);
}

main()
{
   void *Function = (int (*) (int)) Output_Num;
   (*Function) (5);
}

I want to declare an object which is actually a pointer to a
function so I may call it through the other object instead.

I did read K&R's section on pointers to functions but still
could not get this code to work.

One more question, what would the code look like if the function
were:

void Hello_World() {printf("Hello world")}

main()
{
   void *F = (void (*) (void)) Hello_World;  /* can I use () in
					        place of (void)? */
   (*F) ();  /* or should the call be (*F) (void) */


Thanks,
Pat Martin



More information about the Comp.lang.c mailing list