[comp.lang.c] can you 'splain this to me?

booloo@lll-lcc.aRpA (Mark Boolootian) (07/16/87)

~~~~~~
In an article called "The C Programming Language" by Ritchie, et al, there
is an example given that I can't seem to figure out.  The example is supposed
to be a pointer to an array of pointers to functions, each retuning an int
and its declaration is:
			int (*(*funnyarray)[])();

I don't know if it is possible to explain why this is what it is but I would
appreciate any attempts (via email).  I can see what 
			int (*f)()
			   and
			int (*g)[]
are but the composition is confusing to me.  Actually I almost had it there
for a moment but it just now slipped away again.  Thanks in advance.

mb

carm+@andrew.cmu.edu (Rick Chimera) (07/17/87)

> In an article called "The C Programming Language" by Ritchie, et al, there
> is an example given that I can't seem to figure out.  The example is
supposed
> to be a pointer to an array of pointers to functions, each retuning an int
> and its declaration is:
>			int (*(*funnyarray)[])();


Just use the "left-right" rule  to figure out uglies like this.  You find the
C identifier being declared, that's 'funnyarray' in this case.  Then you look
left.  You see a '*'.  Ok, result_so_far <- "pointer to".   Now you look
right.  You see (besides the parens which I assume you know how to deal with,
they just separate nonobvious or override precedences) the '[]' brackets.  So
we have result_so_far <- "pointer to array".  Now you look left again.  You
see another '*'.  We have now result_so_far <- "pointer to array of pointers
to".  Look right again.  You see the '()' parens (which I hope you can
distinguish from precedence parens to know they mean functions....).  Now we
have result_so_far <- "pointer to array of pointers to functions".  You look
left again to see the 'int'.  Now you have result_so_far <- "pointer to array
of pointers to functions returning int".  Looking right we see the ';' so you
are done.  QED.

Rick Chimera
Arpa:   carm+@andrew.cmu.edu
UUCP:   ...!{seismo, ucbvax, harvard}!andrew.cmu.edu!carm+

root@altos86.UUCP (07/17/87)

In article <966@lll-lcc.aRpA> booloo@lll-lcc.UUCP (Mark Boolootian) writes:
>~~~~~~
>In an article called "The C Programming Language" by Ritchie, et al, there
>is an example given that I can't seem to figure out.  The example is supposed
>to be a pointer to an array of pointers to functions, each retuning an int
>and its declaration is:
>			int (*(*funnyarray)[])();
>
>I don't know if it is possible to explain why this is what it is but I would
>appreciate any attempts (via email).  I can see what 
>			int (*f)()
>			   and
>			int (*g)[]
>are but the composition is confusing to me.  Actually I almost had it there
>for a moment but it just now slipped away again.  Thanks in advance.
>
>mb

typedef int (*pfri)();	/* Pointer to Function Returning Int	*/
typedef pfri apfri[];	/* Array of PFRI			*/
typedef apfri *papfri;	/* Pointer to APFRI			*/
	/* which is "funnyarray above" */

Combining, we get the more obscure declaration of `apfri' of
	(*apfri[])()
how?
	1.	take
			(*pfri)()
	2.	subsitute
			apfri[]
		in for "pfri" and get
			(*apfri[])()
		note that in this case extra parentheses are not needed,
		since [] binds more tightly than *

This can also be done with the next step, that of getting the more
obscure declaration of `papfri':
	1.	take
			(*apfri[])()
	2.	subsitute
			*papfri
		in for "apfri" and get
			(*(*papfri)[])()
		note that here the inner set of parens are absolutely
		necessary due to the fact that
			(**name[])()
		binds like
			(**(name[]))()
		which has a very different meaning: an array of pointers
		to functions returning pointers to functions returning
		integers...


typedef int (*pfri)();		/* Pointer to Function Returning Int */
typedef pfri (*pfrpfri)();	/* Pointer to Function Returning PFRI */

int (*(*pfrpfri))() ==		(**pfrpfri)()
array of these ==		(**apfrpfri[])()

--------------------------------------------------
Nathaniel Ingersoll
Altos Computer Systems, SJ CA
...!inhp4!elxsi!altos86!nate
public@ucscd.UCSC.EDU

bapat@utx1.UUCP (The maniac) (07/23/87)

In article <966@lll-lcc.aRpA>, booloo@lll-lcc.aRpA (Mark Boolootian) writes:
> ~~~~~~
> 			int (*(*funnyarray)[])();
> I don't know if it is possible to explain why this is what it is but I would
> appreciate any attempts.

Here is a sample cdecl session:

$ cdecl
explain int(*(*funnyarray)[])()
declare funnyarray as pointer to array of pointer to function returning int

-- 
Subodh       {ihnp4|allegra|codas}!novavax!utx1!bapat         (305) 476-6068
[Subodh Bapat, Racal Milgo, MS E-112, PO Box 407044, Sunrise, FL 33340-7044]

"It is 11 p.m., do you know what your crontab is doing?"

ubi@sri-unix.ARPA (Ron Ueberschaer) (07/25/87)

"The maniac" writes:

>Here is a sample cdecl session:
>
>$ cdecl
>explain int(*(*funnyarray)[])()
>declare funnyarray as pointer to array of pointer to function returning int
>

Well, Mr. Maniac, that sure looks nice.  Where can I find the "cdecl" 
utility?  I tried "man -k"-ing the following:  explain, declare, declar, and
cdecl; none offered a clue.  What environment do you work in?  Please post,
as I'm sure others would love to have this tool.

				--Ron Ueberschaer
				  SRI International	Menlo Park, CA
				  ...!ubi!{hplabs,rutgers}!sri-unix!ubi
				  ubi@sri-unix.uucp

apc@cblpe.ATT.COM (Alan Curtis) (08/02/87)

How do you properly declare a function that returns it's
address:

a function that returns a pointer to a function that returns a pointer to
a function that returns a pointer to a function that returns a pointer to
.
.
.

????

PS This function is easy to implement, since it can simply call itself
	to find out it's address...... :)
--
        This space intentionally left          | Alan P. Curtis 
                  blank                        | AT&T,BTL,CB
                                               | +1 614 860 4749
-----------------------------------------------| apc@cblpe.ATT.COM
Kudos to stargate for redistribution rights    | !cbosgd!cblpe!apc