Reg Function pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sowmi
    New Member
    • Jun 2007
    • 3

    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.Likewis e, 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.


    Below are my code,


    [/code]
    command.h
    **********
    #define FUNCPTR void *
    #define MAX_NUM_COMMAND S 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(c har* command);
    };
    FunctionEntry funArr[MAX_NUM_COMMAND S]=
    {
    {"aaa",(FUNCPTR )aaa},
    {"bbb", (FUNCPTR)bbb},
    {"ccc",(FUNCPTR )ccc}

    };
    enum commandName
    {
    aaa = 0,
    bbb
    };


    command.c
    **********
    void void Execute ()
    {
    .........
    .........
    Execmd execmd;
    FunctionEntry command;
    int rc;
    if(0 != SearchCommand(e xeCmd.command))
    {
    command = FunctionEntry.S earchCommand(ex eCmd.command);
    switch((int)com mand.numOfArgue ments)
    {
    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(c har* command)
    {

    ExeCmd_s exeCmd; //already had a Structure where I am extracting the commandname..
    int index = 0;
    while(index < MAX_NUM_COMMAND S)
    {
    if(strcmp(exeCm d.command,funAr r[index]) == 0)
    {
    return &funArr[index];
    }
    index++;
    }
    return NULL;
    }
    [/code]




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

    Thanks,
    Sowmi
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The first thing to do is remove all of the type casts.

    The second thing to do is read about pointers in a C++ textbook. I recommend "C++ Primer Plus" by Stephen Prata or the "C Primer Plus" by the Waite Group.

    Basicall, a pointer is a variable. However, the value inside the variable can only be the address of something else. You must a) put an address in the pointer and b) you must de-reference the pointer in order to use the something else:

    [code=c]
    int a = 10;
    int* prtr; //this is the pointer
    ptr = &a; //the address of a is put inside the pointer
    cout << ptr; //you see the address inside th pointer
    cout << *ptr; //you see the value of a as 10
    *ptr = 20; //change the value to 20 at the address inside the pointer
    //this changes a to 20
    cout << *ptr; //you see the value of a as 20.
    [/code]

    Get comfortable with this type of pointer before attempting a function pointer.

    There is simple too much wrong with your code for me to fix it.

    Comment

    Working...