Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to call appropriate functions?

Reply
Thread Tools

How to call appropriate functions?

 
 
Joona I Palaste
Guest
Posts: n/a
 
      10-13-2003
Ganesh Kundapur <(E-Mail Removed)> scribbled the following:
> Hi all,
> suppose i have 100 functions such as f1, f2,...f100 in a single
> file including main. If i pass arguement to main such as
> $a.out f1
> main should call appropriate functions. I can do this using switch
> statement, but if i have 1000 functions, then i have to write 1000 cases
> OR i can use pointer to functions. I am passing string to main, how to
> map this string to function efficiently.


If they're all named f{i} where {i} is an integer value from 1 to 100,
then you can use strtol() to find out the value of this integer, and use
it to index an array of function pointers. I don't see any easier way.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Roses are red, violets are blue, I'm a schitzophrenic and so am I."
- Bob Wiley
 
Reply With Quote
 
 
 
 
Ganesh Kundapur
Guest
Posts: n/a
 
      10-13-2003
Hi all,
suppose i have 100 functions such as f1, f2,...f100 in a single
file including main. If i pass arguement to main such as
$a.out f1
main should call appropriate functions. I can do this using switch
statement, but if i have 1000 functions, then i have to write 1000 cases
OR i can use pointer to functions. I am passing string to main, how to
map this string to function efficiently.

 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      10-13-2003
Joona I Palaste wrote:
> Ganesh Kundapur <(E-Mail Removed)> scribbled the following:
> >
> > suppose i have 100 functions such as f1, f2,...f100 in a single
> > file including main. If i pass arguement to main such as
> > $a.out f1
> > main should call appropriate functions. I can do this using
> > switch statement, but if i have 1000 functions, then i have to
> > write 1000 cases OR i can use pointer to functions. I am
> > passing string to main, how to map this string to function
> > efficiently.

>
> If they're all named f{i} where {i} is an integer value from 1
> to 100, then you can use strtol() to find out the value of this
> integer, and use it to index an array of function pointers. I
> don't see any other way.


That does nicely for trying the system out, including getting the
initialization of the function pointers and declaration of the
function type right. Then the OP can improve on it by changing
the array to an array of struct, where the struct holds the
function pointer and a pointer to its name. Then a sequential
search handles arbitrary input strings.

If you pass argv and argc on down, the functions can continue the
process with argv[2], after checking for existence.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
Derk Gwen
Guest
Posts: n/a
 
      10-14-2003
Ganesh Kundapur <(E-Mail Removed)> wrote:
# Hi all,
# suppose i have 100 functions such as f1, f2,...f100 in a single
# file including main. If i pass arguement to main such as
# $a.out f1
# main should call appropriate functions. I can do this using switch
# statement, but if i have 1000 functions, then i have to write 1000 cases
# OR i can use pointer to functions. I am passing string to main, how to
# map this string to function efficiently.

Use a program generator. Write a program that writes the source file.
...
fputs(
"#include <stdio.h>\n"
"int main(int argc,char **argv) {\n"
...
,stdout);
for (i=1,else=""; i<1000; i++,else="else ") {
printf("%sif (strcmp(\"f%d\",argv[1])==0) f%d();",else,i,i);
}
...

If you're using make, you can include program generator
xyz: xyz.o
cc -o xyz xyz.o
xyz.o: xyz.c pqr.h
cc -c xyz.c
xyz.c: xyz-gen
xyz-gen >xyz.c
xyz-gen: xyz-gen.c
cc -o xyz-gen xyz-gen.c

--
Derk Gwen http://derkgwen.250free.com/html/index.html
Don't say anything. Especially you.
 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      10-14-2003
Ganesh Kundapur <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Hi all,
> suppose i have 100 functions such as f1, f2,...f100 in a single
> file including main. If i pass arguement to main such as
> $a.out f1
> main should call appropriate functions. I can do this using switch
> statement, but if i have 1000 functions, then i have to write 1000 cases
> OR i can use pointer to functions. I am passing string to main, how to
> map this string to function efficiently.


You're going to have to implement a lookup table of some sort. If all
of the called functions have the same return type and number of
parameters, you could do something like the following (probably not
the best way of doing it, but I'm doing this off the top of my head):

void f1 (void) {...}
void f2 (void) {...}
void f3 (void) {...}
....
void f100 (void) {...}

struct fassoc {
char name[5]; /* long as longest function name + 1 */
void (*fptr)(void); /* pointer to function */
};

struct fassoc fassoc_table[100] =
{ {"f1", f1}, {"f2", f2}, {"f3", f3}, ..., {"f100", f100} };

void call_func_by_name (char *name)
{
int i;

for (i = 0; i < 100; i++)
{
if (strcmp (name, fassoc_table[i].name) == 0)
{
fassoc_table[i].fptr ();
break;
}
}
}

int main (int argc, char **argv)
{
if (argc == 2)
{
call_func_by_name (argv[1]);
}

return 0;
}

If the functions have different return types or take different numbers
of parameters, then things get a bit trickier. You'd still need a
lookup table, but now you'd need to accomodate several types of
function pointers, and I'm not sure if you could initialize it like
above (you'd probably have to call a function at program startup to
set up the lookup table).
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
MCSE Curriculum - here is what was given, please tell me if this is appropriate. none@set.yet MCSE 7 03-22-2006 11:49 AM
Which event is appropriate.... Nelson ASP .Net 2 11-08-2004 10:39 PM
Requesting suggestions for appropriate use of VLANs Jamie Cisco 1 09-17-2004 08:58 PM
Simple Question on appropriate control??? =?Utf-8?B?VGltOjouLg==?= ASP .Net 1 07-15-2004 10:31 AM



Advertisments