473,795 Members | 2,919 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1996
Ganesh Kundapur <gk*******@yaho o.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.hel sinki.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*******@yaho o.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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 13 '05 #3
Ganesh Kundapur <gk*******@yaho o.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*******@yaho o.com> wrote in message news:<3F******* *******@yahoo.c om>...
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_na me (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_na me (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
1182
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
5907
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 route. The problem being that I can't call these functions from the query designer in Access. I decided that I would try the route of declaring the functions from the dll file the same way you would for the Windows API. Access then complains that...
2
8767
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 PROCEDURE public.match_cond_name_generate();
6
6433
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. Here is a description of the three functions: Insert: Accepts as input the array and the element you want to insert into the array. The function should insert the element at the end of the array and should call the function Sort to sort the...
9
7010
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 buffer; count = getcount(buffer, offset); void* pfun = getmethod(buffer, offset);
11
2406
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
8528
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 here" def func1(): print "hello"
1
2570
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 C code i.e. Connect. This function internally use JNI and call java functions. Now while compiling I am facing problems. I think that there is some problem with my makefile.
10
14442
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 is a managed code. Then I have to call the functions in my software to perform the processing and computing . I understand that I have to wrap the C++ native code with a CLI managed code. How do I do it ? and How do I transfer the parameters ? I...
0
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10443
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9044
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7543
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.