Re: Interesting problem dynamically forming func names ??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nick Keighley

    Re: Interesting problem dynamically forming func names ??

    On 20 Jun, 10:29, onkar.n.maha... @gmail.com wrote:
    Is it possible to from function call on fly in C programming
    language ?
    no
    I am faced with an interesting problem in that I need to take function
    name and arguments on fly (actually from Database as string ) and form
    a function call based on that ?
    >
    something like  FUN_NAME="fun1"  , ARGS ="arg1,arg2,arg 3"  , for this
    pair function call must be
    fun1(arg1,arg2, arg3);
    >
    assume that fun1(int,char,c har*) is defined before the call ?
    >
    i.e.,
    >
    get fun_name,args;
    form a call
    C is statically compiled. By the time it executes everything
    must be there and types are fixed. Dynamically linked libraries
    can enable you to change functionality on the fly but the function
    call interface remains fixed.

    You really need a more dynamic language (Python, scheme etc)
    or you can try and fake it (kludj it) in C. You really end
    up writing an inferior version of scheme/lisp/python/forth.
    Note that often Python etc. can often be called from C
    (and vice versa).

    About the best you can do is

    if (strcmp (fun_name, "fred")
    fred (a, b, c);
    else
    if (strcmp (fun_name, "joe")
    joe(a, b);

    if there are a lot of freds and joes maybe generate
    the switch programitically . Perhaps from the
    database.



    --
    Nick Keighley
  • Ike Naar

    #2
    Re: Interesting problem dynamically forming func names ??

    In article <79c82e06-2731-4185-93ae-5ee3504e13f7@d7 7g2000hsb.googl egroups.com>,
    Nick Keighley <nick_keighley_ nospam@hotmail. comwrote:
    >About the best you can do is
    >
    >if (strcmp (fun_name, "fred")
    If fun_name compares equal to "fred", strcmp will return 0 and the
    if condition will be false. Is that what you intended?
    fred (a, b, c);
    >else
    >if (strcmp (fun_name, "joe")
    Same here.
    joe(a, b);
    Regards,
    Ike

    Comment

    • Nick Keighley

      #3
      Re: Interesting problem dynamically forming func names ??

      On 21 Jun, 01:23, i...@localhost. claranet.nl (Ike Naar) wrote:
      In article <79c82e06-2731-4185-93ae-5ee3504e1...@d7 7g2000hsb.googl egroups..com>,N ick Keighley <nick_keighley_ nos...@hotmail. comwrote:
      About the best you can do is
      >
      if (strcmp (fun_name, "fred")
      >
      If fun_name compares equal to "fred", strcmp will return 0 and the
      if condition will be false.  Is that what you intended?
      >
          fred (a, b, c);
      actually it's not even legal C

      <snip>

      --
      Nick Keighley

      Comment

      Working...