Re: How do I call a function that has name stored in a character

From: Robert Stankowic (pcdoktor_at_netway.at)
Date: 11/29/03


Date: Sat, 29 Nov 2003 14:42:57 +0100


"Richard Heathfield" <dontmail@address.co.uk.invalid> schrieb im Newsbeitrag
news:bq9u8t$583$1@sparta.btinternet.com...
> Robert Stankowic wrote:
>
> <snip>
>
> > FILE *cmd_file = fopen("c:\\functions.txt", "r");
> >
> > if(cmd_file)
> > {
> > /*read a line from your file and extract the functionname*/
> > while(fgets(line, sizeof line, cmd_file))
>
> What if the line is too long to fit into the buffer?

Well, if the OP uses function names of more than 253 characters, he deserves
what he gets. *g,d&r*
Seriously, I was aware of that, but tried to keep the example as simple as
possible and forgot to mention that.
Thank you for pointing it out.

> > {
> > /*Then find a matching name in the array*/
> > for(function_index = 0; function_index < NUM_FUNCTIONS
> > && !strstr(line, funclist[function_index]);
> > function_index++)
> > {
> > }
>
> Linear search is fine if there aren't many functions. If there are quite a
> few, though, it would be better to make sure that the function list is
> sorted by function name (using the string as the key), and doing a binary
> search.

Of course. Also having a twenty page switch statement would not be nice, so
a binary search plus an array of function pointers will certainly be an
advantage in that case. One small problem with the bsearch() approach is,
that bsearch() returns a pointer and some "fiddling around" will be
necessary to convert that to an index into an array.

[OT]Btw. "to an index into an array" sounds a little bit strange in my
ears - is it correct?[/OT]

kind regards
Robert



Relevant Pages

  • Re: Any undefined behavior???
    ... extern float window; ... (My objection is that if window is non-negative, ... the data array are searched equally. ... width is a multiple of sizeof, and bsearch looks ...
    (comp.lang.c)
  • Re: [PATCH 0/5] partitions: Changes to fs/partitions for readability and efficiency
    ... I'm also unconvinced that an array of function pointers is any harder to ... if (err) ... More majordomo info at http://vger.kernel.org/majordomo-info.html ...
    (Linux-Kernel)
  • Re: is there an alternative to strstr
    ... > Can I use bsearch on an array with differrent sized elements. ... You can either use qsort or, ... Dan Pop suggested, a binary search tree. ...
    (comp.lang.c)
  • Re: Is it possible to make void * safer?
    ... that's one big hit when simulating templates over using C++ templates. ... Unless you run it through a C++ compiler, ... That's why I added the function pointers, ... array_push(array, data);} ...
    (comp.lang.c)
  • Re: debugging - help
    ... i am executing on Armulator using AXD debugger. ... What i have is an array of function pointers which get periodically ... I parse this array each time, calling the functions ...
    (comp.lang.c)