Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Problem with pointers in C

  • 29-04-2004 02: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, Registered Users 2, Paid Member Posts: 2,427 ✭✭✭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, Registered Users 2 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,563 ✭✭✭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