PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > calling function in C
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
calling function in C

Réponse
 
LinkBack Outils de la discussion
Vieux 28/05/2008, 11h12   #1
Shiv Ranjan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut calling function in C

Hi,
Lets say I have some 10 functions named -

display1()
display2()
display3()
display4()
......
......
display10()

Assume that all these functions have been defined. Now we have to
modify the program so that whatever function name is entered by
keyboard will be invoked. For example if we are accepting string
display5 from the keyboard then display5() method should be invoked.
The flow should be generic so that the code need not be modified even
though some more functions are added.

If any one has any idea do let me know.

Regards
Shiv
  Réponse avec citation
Vieux 28/05/2008, 11h20   #2
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: calling function in C

Shiv Ranjan said:

> Hi,
> Lets say I have some 10 functions named -
>
> display1()
> display2()
> display3()
> display4()
> .....
> .....
> display10()
>
> Assume that all these functions have been defined. Now we have to
> modify the program so that whatever function name is entered by
> keyboard will be invoked.


Construct a lookup table (e.g. hash table, binary search tree, or something
like that) whose entries are structures containing a key (the name of the
function, expressed as a string) and a payload (a pointer to the function
associated with that string). Yes, you will have to recompile whenever you
add a new entry to the table.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
  Réponse avec citation
Vieux 28/05/2008, 11h24   #3
Bartc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: calling function in C

Richard Heathfield wrote:
> Shiv Ranjan said:
>
>> Hi,
>> Lets say I have some 10 functions named -
>>
>> display1()
>> display2()
>> display3()
>> display4()
>> .....
>> .....
>> display10()
>>
>> Assume that all these functions have been defined. Now we have to
>> modify the program so that whatever function name is entered by
>> keyboard will be invoked.

>
> Construct a lookup table (e.g. hash table, binary search tree, or
> something like that)


Any of these will be overkill for this task I think, and likely to give more
trouble than the main assignment.

A simple linear search should be fine. Especially as it seems there will be
one search per keyboard entry.

--
Bartc


  Réponse avec citation
Vieux 28/05/2008, 13h54   #4
pete
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: calling function in C

Shiv Ranjan wrote:
> Hi,
> Lets say I have some 10 functions named -
>
> display1()
> display2()
> display3()
> display4()
> .....
> .....
> display10()
>
> Assume that all these functions have been defined. Now we have to
> modify the program so that whatever function name is entered by
> keyboard will be invoked. For example if we are accepting string
> display5 from the keyboard then display5() method should be invoked.
> The flow should be generic so that the code need not be modified even
> though some more functions are added.
>
> If any one has any idea do let me know.


/* BEGIN new.c */

#include <stdio.h>
#include <string.h>

void display1(void);
void display2(void);
void display3(void);
void display4(void);
void display5(void);

int main(int argc, char **argv)
{
struct {
char *string;
void (*func)(void);
} table[] = {
{"display1", display1},
{"display2", display2},
{"display3", display3},
{"display4", display4},
{"display5", display5},
{ NULL, NULL}
}, *p = table;

putchar('\n');
if (argc > 1) {
while (p -> string != NULL) {
if (strcmp(argv[1], p -> string) == 0) {
p -> func();
break;
}
++p;
}
if (p -> string == NULL) {
puts("Function not found.");
}
} else {
puts("Not enough argc.");
}
return 0;
}

void display1(void)
{
puts("A");
}

void display2(void)
{
puts("B");
}

void display3(void)
{
puts("C");
}

void display4(void)
{
puts("D");
}

void display5(void)
{
puts("E");
}

/* END new.c */

--
pete
  Réponse avec citation
Vieux 28/05/2008, 15h19   #5
Bartc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: calling function in C


"Szabolcs Borsanyi" <s.borsanyi@sussex.ac.uk> wrote in message
news:20080528131219.GB13592@kroto.pact.cpes.susx.a c.uk...
> On Wed, May 28, 2008 at 07:54:53AM -0500, pete wrote:
>> Shiv Ranjan wrote:
>>> Hi,
>>> Lets say I have some 10 functions named -
>>>
>>> display1()

> [snip]
>>> display10()
>>>

>> struct {
>> char *string;
>> void (*func)(void);
>> } table[] = {
>> {"display1", display1},
>> {"display2", display2},
>> {"display3", display3},
>> {"display4", display4},
>> {"display5", display5},
>> { NULL, NULL}
>> }, *p = table;


> Surely, you will want to define macros:
> #define F(x) {#x,x}
> #define G(x) F(display ##x)
> {G(1),G(2),G(3),{NULL,NULL}}
> rather ther writing everything twice.


I think these are just example function names and bodies. The real names
could be all different. Anyway as this is homework you don't want to appear
too clever.

--
Bartc


  Réponse avec citation
Vieux 28/05/2008, 20h55   #6
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: calling function in C

Bartc said:

> Richard Heathfield wrote:


<snip>

>> Construct a lookup table (e.g. hash table, binary search tree, or
>> something like that)

>
> Any of these will be overkill for this task I think, and likely to give
> more trouble than the main assignment.
>
> A simple linear search should be fine.


A linear search through *what*? Well, gee, a lookup table.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
  Réponse avec citation
Vieux 28/05/2008, 22h52   #7
Bartc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: calling function in C


"Richard Heathfield" <rjh@see.sig.invalid> wrote in message
news:Q4mdnSqGmuFRKqDVnZ2dnUVZ8uWdnZ2d@bt.com...
> Bartc said:
>
>> Richard Heathfield wrote:

>
> <snip>
>
>>> Construct a lookup table (e.g. hash table, binary search tree, or
>>> something like that)

>>
>> Any of these will be overkill for this task I think, and likely to give
>> more trouble than the main assignment.
>>
>> A simple linear search should be fine.

>
> A linear search through *what*? Well, gee, a lookup table.


I was referring to your suggestions of using hash tables and binary trees.
But, did I really have to clarify that?

-- bartc


  Réponse avec citation
Vieux 28/05/2008, 23h22   #8
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: calling function in C

Bartc said:

>
> "Richard Heathfield" <rjh@see.sig.invalid> wrote in message
> news:Q4mdnSqGmuFRKqDVnZ2dnUVZ8uWdnZ2d@bt.com...
>> Bartc said:
>>
>>> Richard Heathfield wrote:

>>
>> <snip>
>>
>>>> Construct a lookup table (e.g. hash table, binary search tree, or
>>>> something like that)
>>>
>>> Any of these will be overkill for this task I think, and likely to give
>>> more trouble than the main assignment.
>>>
>>> A simple linear search should be fine.

>>
>> A linear search through *what*? Well, gee, a lookup table.

>
> I was referring to your suggestions of using hash tables and binary
> trees.


Each of those suggestions is perfectly reasonable, and they offer solutions
that are scalable, whereas a linear search is very much an inferior
stopgap solution - adequate (barely) if all you want is a pass mark for a
college assignment, but not a tool you'd want to use very often for this
problem in the Real World. You see, lookup tables have a habit of growing.
(See "The Practice of Programming" for a fine example of how adopting a
stopgap solution can be costly in the long term.)


> But, did I really have to clarify that?


I am at a loss to understand what you thought you were clarifying.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h26.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,22243 seconds with 16 queries