Type of function returning function.

Dave Eisen dkeisen at Gang-of-Four.Stanford.EDU
Thu Jul 12 02:58:05 AEST 1990


In article <1990Jul10.024205.17382 at media.uucp> rmf at media.uucp (Roger Fujii) writes:
>So, just how does one type a function returning a pointer to a function?
>(other than the obvious void *)
>
>Example:
>
>int foo(buff)
>char	*buff;
>{
>	return atoi(buff);
>}
>
>TYPE bar()
>{
>	return foo;
>}
>

Those who suggested it would be clearer using a typedef are probably
right, but it's no big deal doing it directly either.

As always, declare things the same way they will be used.

In an expression, bar () will be a pointer to a function. To use this,
you would dereference the pointer ---  *bar () ---- and then use it for
a function call  --- (*bar ()) () --- which returns an int. 

int (*bar ()) ()
{
  return foo;
}

should work fine.

I've only used Classic C, but I believe the way it is done in ANSI
C is as follows:


int foo (char *buf)
{
  return atoi (buf);
}

int (*bar (void)) (char *)
{
  return foo;
}

where the type of bar is changed to function taking no arguments
returning a pointer to a function taking a char * and returning
an int.



--
Dave Eisen                      	    Home: (415) 323-9757
dkeisen at Gang-of-Four.Stanford.EDU           Office: (415) 967-5644
1447 N. Shoreline Blvd.
Mountain View, CA 94043



More information about the Comp.lang.c mailing list