Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to declare and use an array of pointer on functions?

how to declare and use an array of pointer on functions?

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bouli
    wrote on last edited by
    #1

    Hello people, I'd like to know how to declare and use an array of pointer on functions? Lets say: void f1() { ... } void f2() { ... } void f3() { ... } typedef struct _toto { int x, y; //how to declare my array of pointers functions[3]; } TOTO; TOTO g_toto; void InitTOTO() { g_toto.x=g_toto.y=0; // how to initialize my array of pointers on functions? g_toto.functions[0]=f1; g_toto.functions[1]=f2; g_toto.functions[3]=f3; } int main() { InitTOTO(); for (int i=0; i<3; i++) //how to call g_toto.functions[i]() } I hope you see what I wish to do... Thanks for giving me the clue :) Best regards. Fred. There is no spoon.

    C D 2 Replies Last reply
    0
    • B bouli

      Hello people, I'd like to know how to declare and use an array of pointer on functions? Lets say: void f1() { ... } void f2() { ... } void f3() { ... } typedef struct _toto { int x, y; //how to declare my array of pointers functions[3]; } TOTO; TOTO g_toto; void InitTOTO() { g_toto.x=g_toto.y=0; // how to initialize my array of pointers on functions? g_toto.functions[0]=f1; g_toto.functions[1]=f2; g_toto.functions[3]=f3; } int main() { InitTOTO(); for (int i=0; i<3; i++) //how to call g_toto.functions[i]() } I hope you see what I wish to do... Thanks for giving me the clue :) Best regards. Fred. There is no spoon.

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      The functions need to have the same prototype (that is, same return type, same number of arguments of same types). Then you will need to typedef your prototype (just an example here): typedef (int)MyFuncPrototype(int, int) Then you can use your function pointer: MyFuncPrototype FuncTable[3]; FuncTable[0] = SomeFunction; .... .... FuncTable[0](5,6); // Just as example

      T 1 Reply Last reply
      0
      • C Cedric Moonen

        The functions need to have the same prototype (that is, same return type, same number of arguments of same types). Then you will need to typedef your prototype (just an example here): typedef (int)MyFuncPrototype(int, int) Then you can use your function pointer: MyFuncPrototype FuncTable[3]; FuncTable[0] = SomeFunction; .... .... FuncTable[0](5,6); // Just as example

        T Offline
        T Offline
        t bast
        wrote on last edited by
        #3

        one idea is to use a function with a pointer to a class holding you parameters. typedef (int)MyFuncPrototype(pParamObject)

        1 Reply Last reply
        0
        • B bouli

          Hello people, I'd like to know how to declare and use an array of pointer on functions? Lets say: void f1() { ... } void f2() { ... } void f3() { ... } typedef struct _toto { int x, y; //how to declare my array of pointers functions[3]; } TOTO; TOTO g_toto; void InitTOTO() { g_toto.x=g_toto.y=0; // how to initialize my array of pointers on functions? g_toto.functions[0]=f1; g_toto.functions[1]=f2; g_toto.functions[3]=f3; } int main() { InitTOTO(); for (int i=0; i<3; i++) //how to call g_toto.functions[i]() } I hope you see what I wish to do... Thanks for giving me the clue :) Best regards. Fred. There is no spoon.

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          See if this thread is of any help.


          "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups