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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Pointer to pointers

Pointer to pointers

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 Posts 3 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.
  • J Offline
    J Offline
    jyngem
    wrote on last edited by
    #1

    I'm trying to implement a list of functions together with their names, like this: typedef struct BinaryFunction { double (*eval)(double, double); char* sign; }; double add(double a, double b) { return (a+b); } char addSign[] = " + "; BinaryFunction Add = { add, addSign }; Finally, the pointe: BinaryFunction** bFunctionList; How do I initialize/use this last pointer?

    H I 2 Replies Last reply
    0
    • J jyngem

      I'm trying to implement a list of functions together with their names, like this: typedef struct BinaryFunction { double (*eval)(double, double); char* sign; }; double add(double a, double b) { return (a+b); } char addSign[] = " + "; BinaryFunction Add = { add, addSign }; Finally, the pointe: BinaryFunction** bFunctionList; How do I initialize/use this last pointer?

      H Offline
      H Offline
      HJo
      wrote on last edited by
      #2

      This is one way of doing it... struct BinaryFunction { double (*eval)(double, double); char* sign; }; double add(double a, double b) { return (a+b); } double subtract(double a, double b) { return (a-b); } int main(int argc, char* argv[]) { BinaryFunction** ppBFunctionList = new BinaryFunction*[2]; char addSign[] = " + "; BinaryFunction Add = { &add, addSign }; ppBFunctionList[0] = &Add; char subSign[] = " - "; BinaryFunction Sub = { &subtract, subSign }; ppBFunctionList[1] = ⋐ for (int i = 0; i < 2; i++) { double dRes = (*ppBFunctionList[i]->eval)(3, 2); printf("%.2f\n", dRes); } return 0; } Regards, /Henrik J

      1 Reply Last reply
      0
      • J jyngem

        I'm trying to implement a list of functions together with their names, like this: typedef struct BinaryFunction { double (*eval)(double, double); char* sign; }; double add(double a, double b) { return (a+b); } char addSign[] = " + "; BinaryFunction Add = { add, addSign }; Finally, the pointe: BinaryFunction** bFunctionList; How do I initialize/use this last pointer?

        I Offline
        I Offline
        Iain Clarke Warrior Programmer
        wrote on last edited by
        #3

        I presume you want something like:

        main ()
        {
        BinaryFunction f [] = { {Add, "+"},
        { Subtract, "-"},
        { Power, "pow"},
        { NULL, NULL}
        };

        double dEval1 = DoAFunctionOne ("Add", 1.0, 2.0, f)
        double dEval2 = DoAFunctionTwo ("Add", 1.0, 2.0, &f)
        

        }

        double DoAFunctionOne (char *name, double A, double B, BinaryFunction *func)
        {
        while (func && func->sign)
        {
        if (!lstrcmpi (name, func->sign))
        {
        if (!func->eval)
        break;
        return func->eval (A,B);
        }
        func++; // Move on to the next one...
        }

        // Raise an exception?
        return 0.0;
        

        }
        double DoAFunctionTwo (char *name, double A, double B, BinaryFunction **func)
        {
        for (int n = 0; func && func [n]->sign; n++)
        {
        if (!lstrcmpi (name, func->sign))
        {
        if (!func [n]->eval)
        break;
        return func [n]->eval (A,B);
        }
        }

        // Raise an exception?
        return 0.0;
        

        }

        Having typed this all, I've got a sneaking suspicion I've just done your homework. If I'm right, take this as an example of how these things work. If you hand it in as your own work then your teacher will become suspicious... If I'm wrong, then I apologise! Iain.

        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