Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!linus!philabs!cmcl2!seismo!umcp-cs!chris From: chris@umcp-cs.UUCP (Chris Torek) Newsgroups: net.lang.c Subject: Re: pointers to functions Message-ID: <2128@umcp-cs.UUCP> Date: Sat, 21-Jun-86 21:06:01 EDT Article-I.D.: umcp-cs.2128 Posted: Sat Jun 21 21:06:01 1986 Date-Received: Mon, 23-Jun-86 03:39:16 EDT References: <237@killer.UUCP> Reply-To: chris@maryland.UUCP (Chris Torek) Organization: University of Maryland, Dept. of Computer Sci. Lines: 40 [I could swear this has been answered before, but here goes:] In article <237@killer.UUCP> toma@killer.UUCP (Tom Armistead) writes: >The delima - Is it possible to cast a 'pointer to' a function? Possible: yes; portable: no. >What I have is a char * that holds the address of a function. >int foo(); >char *addr = foo; >Would it be possible to call the function foo() via the variable addr? Technically, that should be `char *addr = (char *) foo;'. If this happens not to lose important information, you can subsequently successfully use value = (*(int (*)())addr)(arg1, arg2, ...); The cast `(int (*)())', odd-looking though it may be, coerces the type to `pointer to function returning int'. >Secondly is it possible to have an array of type 'pointer to' function. >i.e. >int (*function)()[SIZE]; Certainly it is possible; you just need to construct the proper declaration. For example, feeding the description declare f as array 10 of pointer to function returning int through cdecl produces int (*f[10])() There are at least two programs that do what cdecl does; as I recall, the one I do not have was posted less than three months ago to either net.sources or mod.sources. Cdecl itself was posted to net.sources over a year ago. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516) UUCP: seismo!umcp-cs!chris CSNet: chris@umcp-cs ARPA: chris@mimsy.umd.edu