Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Problem with pointers in C

Options
  • 29-04-2004 3:39pm
    #1
    Closed Accounts Posts: 1,567 ✭✭✭


    /* I've just started to learn C, and I can't get the following code to compile
    for me.
    Its a problem with pointers.
    I want to assign a function to a pointer from input by the user.
    Dev-C++ Compiler says: ANSI C++ forbids implicit conversion from `void *' in assignment
    
    It may be a little lame to you C coders here, but i require your help :)
    What am i doing wrong here? */
    
    #include <stdio.h>
    #include <string.h>
    
    #define NUMBER_OF_FUNCTIONS 3
    
    void one(void);
    void two(void);
    void three(void);
    
    void (*lpFunc)(void);
    
    void usage(char *szModuleName);
    
    char *szFunctions[NUMBER_OF_FUNCTIONS] = { "one","two", "three" };
    void *lpFunctions[NUMBER_OF_FUNCTIONS] = { one, two, three };
    
    int main(int argc, char *argv[])
    {
     int function_count;
    
     if (argc != 2)
        {
        usage(argv[0]);
        return 0;
        }
    
        lpFunc == 0;
    
     for (function_count = 0; function_count < NUMBER_OF_FUNCTIONS; function_count++)
         {
         if (!strcmp(argv[1], szFunctions[function_count]))
            {
             lpFunc = lpFunctions[function_count];      /* compiler error here */
             printf("\nFunction %s selected",szFunctions[function_count]);
             lpFunc();
             break;
            }
         }
         if (lpFunc == 0)
            {
             printf("\nInvalid Function");
            }
    return 0;
    }
    
    void usage(char *szModuleName)
    {
     printf("\nUsage: %s <FUNCTION_NAME> one,two,three", szModuleName);
    }
    
    void one(void)
    {
     printf("\nFirst function called");
    }
    
    void two(void)
    {
     printf("\nSecond function called");
    }
    
    void three(void)
    {
     printf("\nThird function called");
    }
    


Comments

  • Registered Users Posts: 2,426 ✭✭✭ressem


    1
    if (argc != 2)
    {
    usage(argv[0]);
    return 0;
    }

    lpFunc == 0; // guess you mean lpFunc = 0


    2
    void (*lpFunctions[NUMBER_OF_FUNCTIONS])(void) = { one, two, three };


  • Registered Users Posts: 491 ✭✭flav0rflav


    Just to expand on the solution,

    You declared the lpFunctions variable as an array of void pointers. Then you tried to assign one of its members to a function pointer variable. ressem declares the array correctly. Other people may have been tempted to using a cast during the assignment, but that's just masking the compiler error/warning. My own preferred technique is to use a typedef for the pointer-to-function type, as it makes it a little simpler to see the variables and their types.


  • Closed Accounts Posts: 1,567 ✭✭✭Martyr


    Thanks for the help lads, I'll see if all works out later.
    ressem, you were right about assignment.


  • Closed Accounts Posts: 5,564 ✭✭✭Typedef


    How about this?

    #include <stdio.h>
    
    
    void one(void){
    	printf("One called\n");
    return;
    };
    
    void two(void){
    	printf("Two called\n");
    return;
    };
    
    void (*aggregate[])(void)={one,two};
    
    
    int main(int argc,char**argv){
    
    	int a;
    	void (*dref)(void);
    
    	printf("Running\n");
    	for(a=0;a<2;a++){
    		dref=aggregate[a];
    		dref();
    	}
    return 0;
    };
    
    


Advertisement