Functions pointers and stuff.

Tony Olekshy tony at oha.UUCP
Tue Mar 28 21:51:09 AEST 1989


>From article <1715 at leah.Albany.Edu>, by rds95 at 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 at goofy.megatest.UUCP>, by djones at 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 <a.out.h>
#include <stdio.h>

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 at oha.UUCP).



More information about the Comp.lang.c mailing list