Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!caip!ut-sally!pyramid!decwrl!amdcad!kk From: kk@amdcad.UUCP (Krishnakumar K.) Newsgroups: net.lang.c Subject: Re: pointers to functions Message-ID: <12187@amdcad.UUCP> Date: Mon, 30-Jun-86 13:52:09 EDT Article-I.D.: amdcad.12187 Posted: Mon Jun 30 13:52:09 1986 Date-Received: Tue, 1-Jul-86 03:40:23 EDT References: <237@killer.UUCP> Reply-To: kk@amdcad.UUCP (Brad Budlong) Organization: AMD, Sunnyvale, California Lines: 53 In article <237@killer.UUCP> toma@killer.UUCP (Tom Armistead) writes: >Secondly is it possible to have an array of type 'pointer to' function. >i.e. >int (*function)()[SIZE]; >something like the above, I was able to achieve this using this: >struct fct { > int (*func)(); > } f_array[SIZE]; >function() >{ > int x; > printf("offset: "); > scanf("%d",&x); > (*f_array[x].func)(); >} >Is there another way??? > I'm currently involved in writing an interpreter to process numeric expressions. When I encounter a function reference I need to translate between that string and the 'C' function that will perform it. The following seems to work well. I use a binary search of a function table to keep it fast even for a large number of possible functions. typedef struct { char *funcname; double (*func)(); } FUNCELEM; extern double exp(), log(), sin(), cos(); FUNCELEM functable[] = { { "cos", cos }, { "exp", exp }, { "log", log }, { "sin", sin } }; extern double (*binary_search())(); double callfunc(name, arg) char *name; double arg; { double (*func)(); func = binary_search(functable, name); if (func == NULL) return 0.0; else return (*func)(arg); } Brad Budlong.