473,320 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to call appropriate functions?

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.

Nov 13 '05 #1
4 1973
Ganesh Kundapur <gk*******@yahoo.com> 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 (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Roses are red, violets are blue, I'm a schitzophrenic and so am I."
- Bob Wiley
Nov 13 '05 #2
Joona I Palaste wrote:
Ganesh Kundapur <gk*******@yahoo.com> 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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #3
Ganesh Kundapur <gk*******@yahoo.com> 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.
Nov 13 '05 #4
Ganesh Kundapur <gk*******@yahoo.com> wrote in message news:<3F**************@yahoo.com>...
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).
Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: sdhyok | last post by:
PyMat enables to call matlab functions from python. But, what I want is to call python functions from matlab. Is there any library for it? Thanks. Daehyok Shin
7
by: Tim ffitch | last post by:
Hi I have created a VB dll file that contains common functions I use across various projects in VB, Access and Excel. Rather than have to code the functions in each I decided to use the dll...
2
by: Wei Wang | last post by:
Hi, Can I call multiple functions in one trigger? Something like this: CREATE TRIGGER match_cond_name_generate BEFORE INSERT OR UPDATE ON public.predicate_index FOR EACH ROW EXECUTE...
6
by: flash | last post by:
write a program that manipulates arrays of integers. The main program should call three functions: Insert, Delete, and Search. The Insert function should call a function Sort that sorts the array. ...
9
by: Allen | last post by:
The arguments of function is variable. Given function address, argument type and data, how to dynamically call the function? The following is pseudo code. int count = 0; int offset = 0; char...
11
by: sameer.oak | last post by:
I am looking for some comprehensive tutorials on how to write call back functions in C. Can anyone help me? - sameer oak.
7
by: beginner | last post by:
Hi Everyone, I have encountered a small problems. How to call module functions inside class instance functions? For example, calling func1 in func2 resulted in a compiling error. "my module...
1
by: Ahmad Jalil Qarshi | last post by:
Hi, I am develop a dynamic link library in C on AIX that will internally call java functions using JNI. This libaray will be finally used by other C Executable. There is only one function in...
10
by: gilit golit | last post by:
I have developed a software in C++. Now I am trying to add GUI for it . using windows forms. I understand that it is not recommended to write it in C++ So I have started to write it in C#. This...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.