declaring routines which return function pointers

Pete Wilson plw at mgweed.UUCP
Mon Dec 17 14:37:37 AEST 1984


	In the following truly useless code, lint(1) on Sys V only
complains about 'x' being set but not used. The code was derived from
reading the man section on signal(2):

	main()
	{
		int fa();
		int (*fr())();
		int (*x)();
	
		x = fr(fa);
	
	}
	int (*fr(fp))()
	int (*fp)();
	{
		int nfp();
	
		if ( fp )
			return(fp);
		return(nfp);
	}
	nfp()
	{
		return(0);
	}
	fa()
	{
		return(0);
	}

	Unfortunately, K&R is not too clear about functions which return
pointers to functions. Consistance in declarations and definitions is
what keeps lint happy. Notice the declaration of 'fr' in 'main'. It is
declared as a function which returns a pointer to a function. Later in
the file it is defined in the same way.
	Derivation:

	int f();	/* function which returns an int */
	int *f();	/* function which returns a pointer to an int */
	int (*f)();	/* holds a pointer to a function */
	int (*f())();	/* function which returns a pointer to a
			   function which returns an int */
	???		/* holds a pointer to a function which returns
			   a pointer to a function which returns an
			   int???   DON'T ASK!! */
	
	Still not very clear? I agree!! One can get lost in the depths
of parentheses!!

					Pete Wilson
					AT&T-CP Montgomery Works
					Montgomery, IL
					..ihnp4!mgweed!plw



More information about the Comp.lang.c mailing list