Re: [SLUG] [PIG] call a function without knowing its name in C

From: Paul M Foster (paulf@quillandmouse.com)
Date: Sun May 28 2006 - 03:42:36 EDT


Sick Twist wrote:
> I'm writing a little project in C and have hit a wall. Let's say I have
> Functions A, B, C, & D. How can I make A dynamically call B, C or D
> based on a keyword in a text file *without* A knowing the exact names of
> functions B, C or D beforehand?
>
> Is there some trick I can do with #define, enum and function pointers?
> Or perhaps some other way to go about it? The goal is to make it easy to
> add new functions to the program without having to continually modify
> all the functions that might call them.
>
> Perhaps I am overly complicating things with abstraction but I am still
> curious to see if it can be done.
>

If I understand you correctly (and that's a 50/50 proposition), it seems
like you could set up an array of function pointers, and then pass the
array to function A. So that if function A sees keyword 'alfa', it calls
the first member of the array. If it sees 'bravo', it calls the second
member of the function pointer array. Etc. From the problem statement,
it really only sounds like you need one level of indirection for the
function calls.

Does that help?

-- 
Paul M. Foster
-----------------------------------------------------------------------
This list is provided as an unmoderated internet service by Networked
Knowledge Systems (NKS).  Views and opinions expressed in messages
posted are those of the author and do not necessarily reflect the
official policy or position of NKS or any of its employees.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 19:26:45 EDT