on the fringe of C syntax/semantics

Chris Torek chris at mimsy.UUCP
Tue Oct 10 04:59:09 AEST 1989


[desired: a cast to `pointer to function returning int']

>>If I understand the question, you want (int(*)())
[this is correct]

In article <457 at usage.csd.unsw.oz> troy at mr_plod.cbme.unsw.oz (Troy Rollo)
writes:
>Nope - bracketing only works when you have something to group with. that will
>produce exactly the same results as (int *())

This is wrong.  There is something to group here, namely the empty name.
(int *()) is a cast to `function returning pointer to int', which is not
a legal type for a C expression.  (int (*)()) binds the asterisk to the
empty name, yeilding a cast to `pointer to function of unknown arguments
returning int'.

This may be easier to see when considering a variable declaration:

	int i;		/* Int */
	int *pi;	/* Pointer to Int */
	int fi();	/* Function returning Int */
	int *fpi();	/* Function returning Pointer to Int */
	int (*pfi)();	/* Pointer to Function returning Int */
	int *(*pfpi)();	/* Pointer to Function returning Pointer to Int */

To turn each of the above into casts, drop the name and surround the
whole thing with parentheses.  A cast whose `top level' type (first
word in the expansion above) is `function returning ...' is illegal.
The legal casts are thus:

	(int)
	(int *)
	[cannot cast to Func ret Int]
	[cannot cast to Func ret Ptr to Int]
	(int (*)())
	(int *(*)())

One can use the freely-available (possibly public domain) `cdecl'
program to come up with things like

	% cdecl
[nb: the \-newlines here are for the purpose of posting; cdecl takes
a single input line]
	declare foo as pointer to function returning pointer to function \
	returning pointer to array 3 of pointer to function returning \
	pointer to array 4 of array 2 of pointer to pointer to function \
	returning pointer to array 5 of pointer to pointer to char
	char **(*(**(*(*(*(*(*foo)())())[3])())[4][2])())[5]

To turn the above into a cast, remove the name `foo' and add ():

	(char **(*(**(*(*(*(*(*)())())[3])())[4][2])())[5])

Of course, in ANSI C, one should include the types of the arguments to
each of the functions....
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at cs.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list