Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!shadooby!ginosko!aplcen!haven!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: on the fringe of C syntax/semantics Message-ID: <20077@mimsy.UUCP> Date: 9 Oct 89 18:59:09 GMT References: <789@crdos1.crd.ge.COM> <457@usage.csd.unsw.oz> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 58 [desired: a cast to `pointer to function returning int'] >>If I understand the question, you want (int(*)()) [this is correct] In article <457@usage.csd.unsw.oz> troy@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@cs.umd.edu Path: uunet!mimsy!chris