Table lookup for functions..

This is a discussion on Table lookup for functions.. within the C Programming forums, part of the General Programming Boards category; hey guys im trying to make a table look up for a collection of functions so i have chosen to ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    2

    Table lookup for functions..

    hey guys im trying to make a table look up for a collection of functions so i have chosen to use a table look up with a function pointer run by a 2 character string..

    Code:
    const struct Node{
        char cmd[3];                   // type this letter
        void (*fnctPt)(void);};        // to execute this command
    
    
    typedef const struct Node NodeType;
    typedef NodeType * NodePtr;
    
    NodeType TL[]={ // linear linked list
    { "FF", &Forward},
    { "RR", &Reverse},
    { "TL", &Turn_L},
    { "TR", &Turn_R},
    { "RL", &Rotate_L},
    { "RR", &Rotate_R},
    { "HH", &Cmd_OPT}};
    
    
    void Interpreter(char *string){
        NodePtr pt;
        const int table_length = sizeof(TL)/sizeof(*TL);
        int row;
    
    
        for(row=0; row < table_length; row++){
            pt = &TL[row];
            if(strcmp(string, &pt->cmd)){
                pt->fnctPt();
            }
            else{
                SCI_OutString("Error");
            }
        }
    }
    if any of youhave time to try it here is the current project
    https://www.dropbox.com/s/s9lcvzlmax3e8it/proj.zip

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I don't understand this comment:
    Code:
    NodeType TL[]={ // linear linked list
    "Linked" implies pointers to connect the elements.


    You don't need the ampersands in front of the function names.


    This
    Code:
    if(strcmp(string, &pt->cmd)){
    should be
    Code:
    if(strcmp(string, pt->cmd) == 0){

    And you should break out of the loop when a match is found.


    And the error output should happen AFTER the loop, not inside it:
    Code:
        if (row == table_length)
            SCI_OutString("Error");

    Other than that, you didn't really ask a question, so ....
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    2
    Thank you so very MUCH!! One question i have is why would i not need the & in front of the functions if i declared it as a *fnctPt in the struct? excuse my noobness im still learning pointers..

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You don't need the & in front of the function names because the function name itself is already a pointer. The ampersand is redundant but allowed.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,773
    Quote Originally Posted by Samuel Maciel
    why would i not need the & in front of the functions if i declared it as a *fnctPt in the struct?
    The function name as used in that context has function type. It is converted to to a pointer to the function, except when it is the operand of the unary & operator (and also the sizeof operator). Therefore, if you leave out the & you get a pointer to the function, and if you include the & you still get a pointer to the function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lookup table help
    By Ducky in forum C++ Programming
    Replies: 10
    Last Post: 10-27-2011, 12:52 PM
  2. quick log lookup table
    By R.Stiltskin in forum C++ Programming
    Replies: 19
    Last Post: 12-04-2008, 11:39 AM
  3. Making a lookup table?
    By username101 in forum C Programming
    Replies: 7
    Last Post: 05-09-2008, 09:10 AM
  4. using a shared lookup table
    By zxcv in forum C Programming
    Replies: 1
    Last Post: 02-20-2008, 11:10 PM
  5. Lookup Table Reference???
    By gvector1 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2003, 04:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21