functions returning pionters to func - (nf)

mbr at fortune.UUCP mbr at fortune.UUCP
Thu Dec 15 10:24:49 AEST 1983


#R:azure:-243000:fortune:26900002:000:1701
fortune!mbr    Dec 14 12:34:00 1983

The easiest solution would be to use typedef:

	typedef int (*Fint)();

	Fint getroutine(name, table)
	type declaration for name & table;
	{
		.
		.
		.
	}

Your original solution of:

	int (*getroutine())(name, table)

was close.  If you don't want to use typedef, the correct declaration is:

	int (*getroutine(name, table))()

I've tried this on 4.1, and cc is quite happy with it.

Declarations of this complexity frequently get me muddled as well.  I find
the easiest way to think of it is to write the expression in which the
type would appear, successively adding layers until I've reached a simple
type (such as int), and then turn that into the declaration.  For example:

	1. Getroutine is a function, so the expression to  call it would look
	   like:

		getroutine(name, table)

	2. The return value from getroutine is a pointer, so the expression to
	   access its contents is:

		*getroutine(name, table)

	3. The value it points at is the entry point of a function, so the
	   expression to call the function is:

		(*getroutine(name, table))()

	4. The value this function returns is an integer.  The call to this
	   function would use the returned integer for something integerlike:

		i = (*getroutine(name, table))();

	   or:

		if ( (*getroutine(name, table))() == 999)
		{
			/* do something */
		}

	   however, in a type declaration, you just need to state that what
	   you've come up with is an integer and then define your function:

		int (*getroutine(name, table))()
		type declaration for name & table;
		{
			.
			.
			.
		}

			Mark Rosenthal
			{allegra,amd70,cbosgd,dsd,floyd,harpo,hpda,ihnp4,
			 magic,megatest,nsc,oliveb,sri-unix,twg,varian,wdl1}
			!fortune!mbr



More information about the Comp.unix mailing list