Thread: Reg Function Pointers

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    2

    Reg Function Pointers

    Hi All,

    I am trying to implement the concept of function pointer in C++.
    While compiling i got some errors which I couldnt figure out the reason .
    So,Please let me know what changes should be done for resolving that error.

    I am trying to execute one particular command function when the commandis pressed.Likewise, I have 100 commands and whenever the commands are pressed the corresponding function + arguements should be passed and the function pointer should execute the command.

    attached the code,..


    Please let me know where i should change the code.
    Expecting some solutions for this code ASAP .

    Thanks,
    Sowmi

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Expecting some solutions for this code ASAP .
    uh oh.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try using a proper typedef, by reading this -> http://www.newty.de/fpt/index.html
    As opposed to casting everything to void* and hoping for the best.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by indigo0086 View Post
    uh oh.
    That's a great way *not* to get answers =)

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Desolation View Post
    That's a great way *not* to get answers =)
    Or to get sarcastic replies about deathrays.....

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Heh, that's a new one...an attempt at code tags in the attached file.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Here is the attachment as a regular post with code tags (I've fixed the indentation as well):

    command.h
    Code:
    #define FUNCPTR  void *
    #define MAX_NUM_COMMANDS 3
    
    typedef struct
    {
       char command[LINELEN];                           /* name of the command */
       char parm1[LINELEN];                             /*parameter 1 */
       char parm2[LINELEN];                             /*parameter 2 */
                                          
    } ExeCmd_s;
    class FunctionEntry
    {
    
    public:
        char* commandString;
        FUNCPTR pFun;   
        int SearchCommand(char* command);
    };
    FunctionEntry funArr[MAX_NUM_COMMANDS]=
    {
    	    {"aaa",(FUNCPTR)aaa},
                {"bbb", (FUNCPTR)bbb},
       	    {"ccc",(FUNCPTR)ccc}
    
    };
    enum commandName
    {
    	aaa = 0,
    	bbb
    };

    command.c
    Code:
    void void Execute ()
    {
        .........
        .........
        Execmd execmd;
        FunctionEntry command;
        int rc;
        if(0 != SearchCommand(exeCmd.command))
        {
            command = FunctionEntry.SearchCommand(exeCmd.command);
            switch((int)command.numOfArguements)
            {
            case Zero:
                rc = ((command.pFun)();
                break;
            case one:
                rc = ((command.pFun),exeCmd.parm1);
                break;
            }
        }
        else
        {
            printf("No match");
        }
    
    }
    
    //Function to search and returns the index of the command
    int FunctionEntry :: SearchCommand(char* command)
    {
         
        ExeCmd_s exeCmd;  //already had a Structure where I am extracting the commandname..
        int index = 0;     
        while(index < MAX_NUM_COMMANDS)
        {
            if(strcmp(exeCmd.command,funArr[index]) == 0)
            {
                return &funArr[index];
            }
            index++;	
        }
        return NULL;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    Well, first of all, post your errors, second, what's with these arrays?
    Code:
    char command[LINELEN];                         
    char parm1[LINELEN];                           
    char parm2[LINELEN];
    What is LINELEN? It isn't declared.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Also,
    Code:
    FunctionEntry funArr[MAX_NUM_COMMANDS]=
    {
    	    {"aaa",(FUNCPTR)aaa},
                {"bbb", (FUNCPTR)bbb},
       	    {"ccc",(FUNCPTR)ccc}
    
    };
    enum commandName
    {
    	aaa = 0,
    	bbb
    };
    aaa and bbb are declared after you use them, and ccc is not declared at all.
    Code:
             case Zero :
             case one :
    Use persistent capitalization! It will save you many headaches.
    Code:
    int FunctionEntry :: SearchCommand(char* command)
    {
         /* ... */
         return NULL;
    }
    NULL isn't an int. It's something you put into a pointer. Return 0 or change the return type.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class function pointers
    By VirtualAce in forum C++ Programming
    Replies: 40
    Last Post: 02-17-2005, 12:55 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. function pointers and member functions
    By thor in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2002, 04:22 PM