Using vars to access structures/functions

Doug Gwyn gwyn at smoke.BRL.MIL
Fri Jul 21 09:16:19 AEST 1989


In article <329 at yetti.UUCP> asst-jos at yetti.UUCP (Jeffrey Klein) writes:
>	Is it possible to place the name of a field in a variable, and
>then use that variable to reference the field?

Not as such, since source code identifiers vanish before run time.
(Except for possible debugging or interpreter environments, but the
identifiers are still not supposed to be available to the program.)

You need to design some other solution.  For function tables, you
might set up something like:
	extern void func1(), func2();
	struct func_pair {
		char *func_name;
		void *func_entry();
	} funct_table[] = {
		{ "func1", func1 },
		{ "func2", func2 },
	};
and search the table for a matching func_name member, then use the
struct func_pair pointer that matched to invoke the function via the
corresponding funct_entry member.  Selecting different members of a
struct by name is a harder proposition, and it would be best to find
some other way of accomplishing the desired effect in such a case.



More information about the Comp.lang.c mailing list