strings as functions
D.S.Riches
dsr at stl.stc.co.uk
Wed Jan 9 20:15:19 AEST 1991
>In article <mpapp.663214690 at godzilla> mpapp@ (Mike Papper) writes:
>Is there a way to use a string value as the name of a function?
>In other words, can I use a string (possibly
>typed in by a programmer) as a function call to a function of
>the same name?
Tried to reply to you Mike but your path name is corrupted. See your
Newsmaster about it.
It seems similar to the implementation of a State Transition Model or
Table Driven Software. E.g.
We define a structure :-
typedef struct sy_s_bed_add_nw_object {
gen_t_nw_object nw_object;
STRING form_name;
INT (*func_name)();
INT aux_row;
} sy_s_bed_add_nw_object;
Declare a table for it :-
NE_D_EXTERN sy_s_bed_add_nw_object sy_g_bed_add_table[]
#ifdef DEFINITION
= {
GEN_D_SITE, "adsite", sy_f_bed_Add_Geog_Object, 0,
GEN_D_BLDG, "adbldg", sy_f_bed_Add_Geog_Object, 2,
GEN_D_LINK, "adlink", sy_f_bed_Add_Tx_Object, 3,
GEN_D_LINK_PT, "undefined", dummy, -1,
GEN_D_NODE, "adnode", sy_f_bed_Add_Default_Object,
4,
}
#endif
In this case the structure defines a pointer to a function as the 3rd
element. In the table we have a name of a function. We then call
that function by :-
status = (*(sy_g_bed_add_table[i_object_type].func_name))(m_cfmPtr);
Where i_object_type is the 'line_number' corresponding to the nw_object
we're interested in (in State Transitions this would be
the trigger, we search down a table for the trigger. move across to the action
(funtion) to be called and execute it).
m_cfmPtr is the parameter to this function.
This is taken from a 'live' example, what you might need is similar in
that you could assign a string to the 3rd element and then execute it
... I think.
Dave Riches
PSS: David.S.Riches at stl.stc.co.uk (or dsr at stl.stc.co.uk)
Smail: Systems Methods, (Dept. 607, T2 West),
STC Technology Ltd., London Road,
Harlow, Essex. CM17 9NA. England
Phone: +44 (0)279-29531 x2496
More information about the Comp.lang.c
mailing list