function name as string

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

    function name as string

    I think the answer to this is "no" but I figured I'd try....

    I have a function name as a string:

    funcName = "xyz";

    and I have a function:

    void xyz(blah) {}

    Is there some way to call function xyz using the string contained in
    funcName?

    Something like (I know this is wrong)

    (void)(*funcNam e)(blah)???

    --Yan
  • Karl Malbrain

    #2
    Re: function name as string

    On Apr 19, 11:41 am, CptDondo <y...@NsOeSiPnA eMr.comwrote:
    I think the answer to this is "no" but I figured I'd try....
    >
    I have a function name as a string:
    >
    funcName = "xyz";
    >
    and I have a function:
    >
    void xyz(blah) {}
    >
    Is there some way to call function xyz using the string contained in
    funcName?
    >
    Something like (I know this is wrong)
    >
    (void)(*funcNam e)(blah)???
    >
    --Yan
    Sure, just make a table that maps the string name to the function
    pointer, look up the string name, and call the corresponding
    function.

    karl m

    Comment

    • =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=

      #3
      Re: function name as string

      CptDondo wrote:
      I think the answer to this is "no" but I figured I'd try....
      >
      I have a function name as a string:
      >
      funcName = "xyz";
      >
      and I have a function:
      >
      void xyz(blah) {}
      >
      Is there some way to call function xyz using the string contained in
      funcName?
      >
      Something like (I know this is wrong)
      >
      (void)(*funcNam e)(blah)???
      When you run your program, you cannot rely on the names of functions
      even being available at all, let alone having easy access to them,
      sorry. You will need to save the names of all functions you wish to
      call by name, and their addresses, manually.

      struct function {
      char *name;
      void (*func)(void);
      } functions[] = {
      ...
      { "xyz", &xyz },
      ...
      };

      You can then search in this list to see if the name exists, and if so,
      call it.

      name = "xyz";
      for (i = 0; i != sizeof functions / sizeof *functions; i++) {
      if (strcmp(functio ns[i], name) == 0) {
      functions[i].func();
      break;
      }
      }

      (There are almost certainly better ways to search through the list,
      and perhaps better ways to store the list, but the best way depends on
      your needs.)

      Comment

      • CptDondo

        #4
        Re: function name as string

        Harald van Dijk wrote:
        CptDondo wrote:
        >I think the answer to this is "no" but I figured I'd try....
        >>
        >I have a function name as a string:
        >>
        >funcName = "xyz";
        >>
        >and I have a function:
        >>
        >void xyz(blah) {}
        >>
        >Is there some way to call function xyz using the string contained in
        >funcName?
        >>
        >Something like (I know this is wrong)
        >>
        >(void)(*funcNa me)(blah)???
        >
        When you run your program, you cannot rely on the names of functions
        even being available at all, let alone having easy access to them,
        sorry. You will need to save the names of all functions you wish to
        call by name, and their addresses, manually.
        >
        struct function {
        char *name;
        void (*func)(void);
        } functions[] = {
        ...
        { "xyz", &xyz },
        ...
        };
        >
        You can then search in this list to see if the name exists, and if so,
        call it.
        >
        name = "xyz";
        for (i = 0; i != sizeof functions / sizeof *functions; i++) {
        if (strcmp(functio ns[i], name) == 0) {
        functions[i].func();
        break;
        }
        }
        >
        (There are almost certainly better ways to search through the list,
        and perhaps better ways to store the list, but the best way depends on
        your needs.)
        >
        That's kind of what I figured.... I was trying to avoid the mother of
        all switch statements... Or lookups, or whatever. Just trying to make
        the next guy's job a bit easier.... :-)

        Thanks,

        --Yan

        Comment

        • Jack Klein

          #5
          Re: function name as string

          On Thu, 19 Apr 2007 12:04:46 -0700, CptDondo <yan@NsOeSiPnAe Mr.com>
          wrote in comp.lang.c:
          Harald van D?k wrote:
          CptDondo wrote:
          I think the answer to this is "no" but I figured I'd try....
          >
          I have a function name as a string:
          >
          funcName = "xyz";
          >
          and I have a function:
          >
          void xyz(blah) {}
          >
          Is there some way to call function xyz using the string contained in
          funcName?
          >
          Something like (I know this is wrong)
          >
          (void)(*funcNam e)(blah)???
          When you run your program, you cannot rely on the names of functions
          even being available at all, let alone having easy access to them,
          sorry. You will need to save the names of all functions you wish to
          call by name, and their addresses, manually.

          struct function {
          char *name;
          void (*func)(void);
          } functions[] = {
          ...
          { "xyz", &xyz },
          ...
          };

          You can then search in this list to see if the name exists, and if so,
          call it.

          name = "xyz";
          for (i = 0; i != sizeof functions / sizeof *functions; i++) {
          if (strcmp(functio ns[i], name) == 0) {
          functions[i].func();
          break;
          }
          }

          (There are almost certainly better ways to search through the list,
          and perhaps better ways to store the list, but the best way depends on
          your needs.)
          >
          That's kind of what I figured.... I was trying to avoid the mother of
          all switch statements... Or lookups, or whatever. Just trying to make
          the next guy's job a bit easier.... :-)
          >
          Thanks,
          DO NOT use a switch statement for this. Put the entries in the array
          in sorted order and use bsearch().

          --
          Jack Klein
          Home: http://JK-Technology.Com
          FAQs for
          comp.lang.c http://c-faq.com/
          comp.lang.c++ http://www.parashift.com/c++-faq-lite/
          alt.comp.lang.l earn.c-c++

          Comment

          • limkatkat

            #6
            Re: function name as string

            Use a map?
            like this?
            map_get("abc");

            On Apr 20, 2:41 am, CptDondo <y...@NsOeSiPnA eMr.comwrote:
            I think the answer to this is "no" but I figured I'd try....
            >
            I have a function name as a string:
            >
            funcName = "xyz";
            >
            and I have a function:
            >
            void xyz(blah) {}
            >
            Is there some way to call function xyz using the string contained in
            funcName?
            >
            Something like (I know this is wrong)
            >
            (void)(*funcNam e)(blah)???
            >
            --Yan

            Comment

            • websnarf@gmail.com

              #7
              Re: function name as string

              On Apr 19, 11:41 am, CptDondo <y...@NsOeSiPnA eMr.comwrote:
              I think the answer to this is "no" but I figured I'd try....
              >
              I have a function name as a string:
              >
              funcName = "xyz";
              >
              and I have a function:
              >
              void xyz(blah) {}
              >
              Is there some way to call function xyz using the string contained in
              funcName?
              >
              Something like (I know this is wrong)
              >
              (void)(*funcNam e)(blah)???
              You have to have a mapping from "xyz" to void (*xyz)(). The language
              doesn't provide a natural way to do this, but there is a neat and
              maintainable way of doing this. In one file, lets call it funcs.i,
              write the following:

              funcs.i:
              ----------
              functionInvoke( xyz)
              functionInvoke( anyotherfunctio nnameyoulike)
              ....

              And from your C sources do the following:


              program.c:
              ---------------

              #define str_aux(x) #x
              #define str(x) str_aux(x)
              #define functionInvoke( x) str(x), &(x)
              struct functionTable {
              char * name;
              void (* fn) ();
              } fTable[] = {
              #include "funcs.i"
              };
              #undef functionInvoke

              So then you can add as many functions as you like to your table in the
              funcs.i file. You can then search for the string name in fTable
              amongst the names to determine which fn to call.

              --
              Paul Hsieh
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.



              Comment

              Working...