array of function pointers ..

This is a discussion on array of function pointers .. within the C Programming forums, part of the General Programming Boards category; I have program which has an array of function pointers. Code: #include <stdio.h> int (* arrFunPtr[5])(void); void f1(void); void f2(void); ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    108

    array of function pointers ..

    I have program which has an array of function pointers.
    Code:
    #include <stdio.h>
    
    int (* arrFunPtr[5])(void);
    
    void f1(void);
    void f2(void);
    void f3(void);
    void f4(void);
    void f5(void);
    
    void f1()
    {
            printf("Iam in f1 \n");
    }
    
    void f2()
    {
            printf("Iam in f2 \n");
    }
    
    
    void f3()
    {
            printf("Iam in f3 \n");
    }
    
    
    void f4()
    {
            printf("Iam in f4 \n");
    }
    
    
    void f5()
    {
            printf("Iam in f5 \n");
    }
    
    int main()
    {
            int choice=0;
    
            arrFunPtr[0] = &f1; // line 43
    
            printf("Please Enter Your Choice  \n");
            scanf("%d",&choice);
            switch(choice)
            {
                    case 1:
                                            printf("zero \n");
                                            arrFunPtr[choice-1]();
                                            break;
                    default:
                                               printf("Default \n");
                                                 break;
          }
    
            return 0;
    }
    When i compile it , gives me an warning as ..

    ex6.c: In function `main':
    ex6.c:43: warning: assignment from incompatible pointer type


    When i execute it , gives me the desired result.

    Iam concerned about the warming .

    Can any one let me know about this.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your function pointer array is expecting a return type of int, whilst your f1, f2 ... functions are have a void return type. Fix that and you will not have a warning.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  4. Passing a 2d Array of pointers to a Function
    By miclus in forum C Programming
    Replies: 6
    Last Post: 09-11-2004, 07:34 AM
  5. Array of function pointers?
    By The V. in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2001, 08:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21