Sick Twist wrote:
>> From: Paul M Foster <paulf@quillandmouse.com>
>> Reply-To: slug@nks.net
>> To: slug@nks.net
>> Subject: Re: [SLUG] [PIG] call a function without knowing its name in C
>> Date: Sun, 28 May 2006 03:42:36 -0400
>>
>> 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
>
> That certainly sounds very close to what I would like but I still need
> to figure out a way to convert the keyword read from the file to the
> appropriate index number for the array of function pointers while only
> needing to keep one list of keywords in the program:
>
> /* ideally keep keywords in one place */
> #DEFINE ANIMALS LION, TIGER, BEAR
>
> enum {
> ANIMALS,
> TOTAL }
>
> /* set up array of function pointers */
> void (*func_ptr_array[TOTAL]) (void);
>
> void func1 () {
> ...
> /* call me for LION */
> func_ptr_array[LION] = &func1;
> ...
> }
>
> void func2 () {
> ...
> /* call me for TIGER */
> func_ptr_array[TIGER] = &func2;
> ...
> }
>
> void func3 () {
> ...
> /* call me for BEAR */
> func_ptr_array[BEAR] = &func3;
> ...
> }
>
> void main () {
> ...
> /* This is where I am stuck. How do I use ANIMALS and TOTAL
> somehow for main to call the appriate function based on what it finds
> in a file. I know I need some sort of string->integer lookup but I
> just can't seem to figure it out:
>
> animal_string = file_file_contents ();
> ???
> func_ptr_array[animal_integer];
> ...
> }
>
> I thought about changing the #DEFINE to something like this
>
> #DEFINE ANIMALS (ARG) LION ARG TIGER ARG BEAR
>
> so that it could be passed a , from enum and "," from main. That way
> main could use ANIMALS to initialize an array of strings for the lookup.
> Is there a more simple way to achieve the same thing?
>
I've seen something like this done before, but it was done by keeping an
array of words and a separate enum that had to stay in sync.
typedef enum indexes {
ALFA,
BRAVO,
CHARLIE,
DELTA,
ECHO,
FOXTROT
};
const char words {
"alfa",
"bravo",
"charlie",
"delta",
"echo",
"foxtrot",
NULL
};
Ultimately, the programmer wrote an external program to generate header
files based on a single input file. That is, the user would create a
text file with what he needed in it, and the program would generate
these header files based on it, which you'd reference in your program on
a recompile. The reference is "C Database Development" by Al Stevens. He
also wrote "C++ Database Development", but I don't recall the C++ using
the same technique. Anyway, the purpose of the external program was
(among other things) to keep these two arrays in sync.
You could probably eliminate the enum and change it to an array of
function pointers. Personally, I've never had to do this kind of thing,
and run in roiling terror at the idea of function pointers, so I can't
provide a real example of doing this. I'd probably have to study for
half an hour just to remember the syntax for function pointers (shiver).
In any case, you could iterate over the array of "words" until you find
a match. Something like:
/* word on text file is the variable "srch" */
i = 0;
found = 0;
while (words[i] != NULL) {
if (0 == strstr(srch, words[i])) {
found = 1;
break;
}
i++;
}
/* i = index of the word found (or not ;-)
* fp is an array of function pointers
*/
if (found) {
fp[i];
}
Something like that. Sorry, I've been writing PHP for months and I tend
to forget precise C syntax after a while if I haven't coded in it.
(BTW, change your call to void main() to int main(). The standard says
it has to return an int, and people bitch if you don't.)
-- 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:27:11 EDT