Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!ucsd!rutgers!att!alberta!oha!tony From: tony@oha.UUCP (Tony Olekshy) Newsgroups: comp.lang.c Subject: Re: Functions pointers and stuff. Message-ID: <301@oha.UUCP> Date: 28 Mar 89 11:51:09 GMT References: <1715@leah.Albany.Edu> <3182@goofy.megatest.UUCP> Organization: Olekshy Hoover & Associates Ltd., Edmonton, Alberta, Canada Lines: 47 From article <1715@leah.Albany.Edu>, by rds95@leah.Albany.Edu (Robert Seals): > > Is there some sneaky way to execute the function when all I know is > the name of it, i.e., a pointer to several letters which spell it's name. And from article <3182@goofy.megatest.UUCP>, by djones@megatest.UUCP (Dave Jones): > > There's no portable way. What system are you running? What version... And from the SVID (quasi-portable, if you have SV or Xenix or ?): ---------------------------------------------------------------- #include #include struct nlist nameinfo[] = { {"_f_name"}, {""} }; main() { if (nlist(*argv, nameinfo) == -1) { fprintf(stderr, "Bad name list or function name.\n"); exit (1); } /* Should check nameinfo[0].n_type here, but eventually... */ (*(int (*)())(nameinfo[0].n_value))(); exit (0); } f_name() { printf("f_name called!\n"); return (0); } ------------------------------------------------------------- Note that this is pretty slow on some machines, so you will want to cache the data somehow. You can check the mod time on *argv to invalidate cache entries. On the 286 this requires xlist(), then shift segment, or offset, cast to pointer to function returning int, and dereference. Have fun. Yours, etc., Tony Olekshy (...!alberta!oha!tony or tony@oha.UUCP).