Thread: Function pointer issue

  1. #1
    Registered User
    Join Date
    Feb 2020
    Posts
    3

    Function pointer issue

    Hi, i have a problem with my code. Currently i am trying to learn function pointers and the best way is practicing but my little program don't work as it should. After i call (*execute) it does not show anything and i don't get what's wrong. execute is assgined to point to a function by pick_function. Maybe could someoane look on to my code:



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef void (*fptr)(void);
    
    
    void func1(void)
    {
        printf("\n\t\t Message 1\n\n");
    }
    void func2(void)
    {
        printf("\n\t\t Message 2\n\n");
    }
    void func3(void)
    {
        printf("\n\t\t Message 3\n\n");
    }
    void func4(void)
    {
        printf("\n\t\t Message 4\n\n");
    }
    
    
    
    
    fptr pick_function(fptr array_of_fptr[])
    {
        int insert;
        printf("\n\n\t\t Choose a message between 0-6 \n\n");
    
    
        do scanf("%d",&insert);
        while((insert<0)||(insert>3));
    
    
        switch (insert)
            {
            case 0:
                return array_of_fptr[0];
                break;
            case 1:
                return array_of_fptr[1];
                break;
            case 2:
                return array_of_fptr[2];
                break;
            case 3:
                return array_of_fptr[3];
                break;
            }
    }
    
    
    
    
    int main()
    {
    
    
        fptr array_of_fp[4]={func1,func2,func3,func4},execute;
        execute=pick_function(array_of_fp);
        (*execute);
    
    
    }



    Thanks in advance!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to call the function through the function pointer:
    Code:
    (*execute)();
    It should also be possible to omit the explicit dereference as a form of syntactic sugar:
    Code:
    execute();
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2020
    Posts
    3
    It worked. Thanks very much!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with function pointer and template
    By CodeSlapper in forum C++ Programming
    Replies: 6
    Last Post: 08-18-2016, 10:48 PM
  2. issue pointer in c
    By harryloke in forum C Programming
    Replies: 7
    Last Post: 10-12-2013, 06:50 PM
  3. Pointer to Pointer manipulation issue
    By workisnotfun in forum C Programming
    Replies: 5
    Last Post: 10-16-2012, 11:31 AM
  4. Replies: 3
    Last Post: 03-13-2012, 12:15 PM
  5. function pointer issue
    By drshmoo in forum C Programming
    Replies: 7
    Last Post: 09-22-2011, 03:04 PM

Tags for this Thread