Thread: Array of function pointers

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    224

    Array of function pointers

    I'm having trouble creating an array of function pointers. Here's what I have:

    Code:
    int first_fit(int, int);
    int best_fit(int, int);
    int worst_fit(int, int);
    int (*part_algo_array)[3](int, int);
    part_algo_array[0] = first_fit;
    part_algo_array[1] = best_fit;
    part_algo_array[2] = worst_fit;
    I get these compiler errors:
    main.c:17: ANSI C++ forbids declaration `part_algo_array' with no type
    main.c:17: invalid initializer
    main.c:18: ANSI C++ forbids declaration `part_algo_array' with no type
    main.c:18: conflicting types for `int part_algo_array[1]'
    main.c:17: previous declaration as `int part_algo_array[0]'
    main.c:18: invalid initializer
    main.c:19: ANSI C++ forbids declaration `part_algo_array' with no type
    main.c:19: conflicting types for `int part_algo_array[2]'
    main.c:18: previous declaration as `int part_algo_array[1]'
    main.c:19: invalid initializer
    main.c: In function `int main(int, char **)':
    main.c:115: `part_algo_array[part_algo]' cannot be used as a function

    I the same error when I place '&' before the function names (part_algo_array[0] = &first_fit; )

    Can anyone help me out. Thanks.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    int (*part_algo_array[3])(int, int);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So why are you using a C++ compiler (check your error messages) if you're posting on the C board.

    Code:
    #include <stdio.h>
    int first_fit(int a, int b){return a+b;}
    int best_fit(int a, int b){return a+b;}
    int worst_fit(int a, int b){return a+b;}
    typedef int (*fnptr)(int,int);
    int main( int argc, char *argv[] ) {
        int (*part_algo_array[3])(int, int);
        fnptr array2[3];
        part_algo_array[0] = first_fit;
        part_algo_array[1] = best_fit;
        part_algo_array[2] = worst_fit;
        array2[0] = first_fit;
        array2[1] = best_fit;
        array2[2] = worst_fit;
        return 0;
    }
    When it comes to function pointers, you can make life a lot easier with a suitable typedef.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    Thank you for replying.
    Although I am writing my code in C++, those three functions are just C functions, so I decided to post here. I'm sorry if I was disruptive.
    And, yes, Salem, your solution works. The weird thing is that when I put the code you had in main before the call to main, main.cc did not compile. Is there a reason for this?

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Is there a reason for this?
    Yes. The best place to start looking is the compiler output. If you don't understand what it's saying, post the code and the errors here.

    However, based on what you said, have you put code outside a function? Code cannot live in global space, only inside a function.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Salem
    When it comes to function pointers, you can make life a lot easier with a suitable typedef.
    Probably the only reason I can ever see myself using typedef. I think it was created for this one purpose alone.

    Quzah.
    Hope is the first step on the road to disappointment.

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, 10: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