Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83 (MC830919); site kvvax4.UUCP Path: utzoo!linus!philabs!cmcl2!seismo!mcvax!kvport!kvvax4!pete From: pete@kvvax4.UUCP (Peter J Story) Newsgroups: net.lang.c Subject: Re: Function returning pointer to function ? (80 lines) Message-ID: <635@kvvax4.UUCP> Date: Thu, 18-Jul-85 15:19:46 EDT Article-I.D.: kvvax4.635 Posted: Thu Jul 18 15:19:46 1985 Date-Received: Sun, 21-Jul-85 01:11:50 EDT References: <11582@brl-tgr.UUCP> Organization: Kongsberg Vaapenfabrikk, Defence Division, Norway Lines: 81 > Replies to me please, unless you feel the net needs a diversion > from the debating on matters of style and efficiency. It certainly does! In any case that ARPA gap always defeats my mail. > Is it legal for a function to return a pointer to a function? Yes, here is some playing that I did, together with a similar problem which I have never solved. Maybe someone can help. /* I had better apologise for the cryptic naming, but in the */ /* interest of compactness... The basic rule for declarations */ /* is to try to figure out how you would access the basic object */ /* from an object of the type you are trying to declare, and */ /* make the declaration look like that. It often helps to build */ /* it up from smaller units, thus: */ int a = 6; int fi() {return(a);} /* fn returning an int */ int *fpi() {return(&a);} /* fn returning ptr to int */ int (*pfi)() = fi; /* ptr to fn returning int */ /* Now the tricky bit. Imagine I had a function which returned */ /* a pointer to a function returning an int. Then I could */ /* assign its result to pfi, thus: pfi = gfpfi(); */ /* Put this in the declaration of pfi above, and you are there */ int (*gfpfi())() {return(pfi);}; /* fn returning ptr to fn returning int */ /* a step further? */ int *(*pfpi)() = fpi; /* ptr to fn returning ptr to int */ int *(*gfpfpi())() {return(pfpi);}; /* fn rtning ptr to fn rtning ptr to int */ main() { int x; x = (*pfi)(); printf("%d",x); x = *(*pfpi)(); printf("%d",x); x = (*gfpfi())(); printf("%d",x); x = *(*gfpfpi())(); printf("%d",x); printf("\n"); myproblem(); /* see below !! */ } /* ---------------------------------------------------------- */ /* Now my turn to ask a question. Here are the declarations: */ char *f1() { return("1"); } char *f2() { return("2"); } /* decl array of ptrs to fns returning ptr to char */ char *(*funcs[2])() = {f1,f2}; /* decl ptr to array of ptrs to fns returning ptr to char */ char *(*(*pfuncs)[])() = funcs; /* "junk.c", line 47: warning: illegal pointer combination */ /* I can't get that to compile without a warning but it works ok. */ /* It should be analogous to this, which also won't compile */ /* without a warning. */ char cd[2] = {'c','d'}; char (*pcd)[] = cd; /* "junk.c", line 55: warning: illegal pointer combination */ /* What is it that the compiler (4.1 BSD) doesn't like? */ myproblem() { int i; for (i=0; i<2; i++) printf("%c",*(*funcs[i])()); /* 12 */ for (i=0; i<2; i++) printf("%c",*(*(*pfuncs)[i])()); /* 12 */ printf("%c",(*pcd)[0]); /* c */ printf("\n"); } -- Pete Story {decvax,philabs}!mcvax!kvport!kvvax4!pete A/S Kongsberg Vaapenfabrikk, PO Box 25, N3601 Kongsberg, Norway Tel: + 47 3 739644 Tlx: 71491 vaapn n