can this be done with fuction pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sanddune008
    New Member
    • Nov 2006
    • 4

    can this be done with fuction pointers

    Code:
    void addNode(void);
    void delNode(void);
    void modifyNode(void);
    void updateNode(void);
    void clarifyNode(void);
    void (*calback)();	
    
    long functionTable[]=
    {
    	addNode,
        delNode,
        modifyNode,
        updateNode,
        clarifyNode,
    };
    
    int main(void)
    {
    	unsigned int sel;
    	printf("Enter choice:");
        scanf("%d",&sel);
        if(sel<(LAST));
    		calback(functionTable[sel]);
    }
    
    void addNode()
    {
    		printf("addNode\n");
    
    }
    void delNode()
    {
    		printf("delNode\n");
    
    }void modifyNode()
    {
    		printf("modifyNode\n");
    
    }
    void updateNode()
    {
    		printf("updateNode\n");
    
    }
    void clarifyNode()
    {
    		printf("clarifyNode\n");
    
    }
    I am getting following warnings
    Cos I have declared function to be void
    'initializing' : 'long ' differs in levels of indirection from 'void (__cdecl *)(void )'

    1. In my case what should be the datatype of the array of function pointers
    functionTable[]


    2. If I am taking a void function as my argument in the function pointer how should i declare him.void (*calback)(?);

    Thanks to all
    Last edited by Banfa; Apr 3 '09, 02:49 PM. Reason: Added
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by sanddune008
    I am getting following warnings
    Cos I have declared function to be void
    'initializing' : 'long ' differs in levels of indirection from 'void (__cdecl *)(void )'

    1. In my case what should be the datatype of the array of function pointers
    functionTable[]


    2. If I am taking a void function as my argument in the function pointer how should i declare him.void (*calback)(?);

    Thanks to all
    This can be done with function pointers, the answer to question 1 is in the warning you have posted. The data type of each member of the array needs to be void (*)(void), a function pointer to a function that takes no parameters and returns nothing not long, it would be declared as

    void (*FunctionTable[])(void) = { ... };

    Personally I find function pointers a little confusing so to simplfy this a little I normally typedef my function pointer type before declaring the array

    typedef void (*FNVOIDRVOID)( void);

    then the table becomes

    FNVOIDRVOID FunctionTable[] = { ... };

    Your declaration and use of callback is flawed. It is not required you make the function call using functioFunction Table like this

    (FunctionTable[0])();

    Obviously changing the index as required.

    You have not declared LAST

    For a simple case like your example I would probably use a simple switch statement.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      What kind of function has no arguiments (void) and returns nothing (void)?

      That is, there are functions wioth no input and no output?

      Or, I suspect you are using global variables, which is a big no-no.

      Read this: http://bytes.com/topic/c/insights/73...obal-variables

      Comment

      Working...