Function Arrays

This is a discussion on Function Arrays within the C++ Programming forums, part of the General Programming Boards category; Is it possible to make an array of functions? eg funcarray[2](); and code the functions later?...

  1. #1
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469

    Function Arrays

    Is it possible to make an array of functions? eg funcarray[2]();
    and code the functions later?

  2. #2
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    err.... nope

    If you want to declare a function and "code it later" (aka define it). You can build a prototype ot it.

    int sum(int a, int b); //this is the prototype. Note the semicolon

    //lot's of code goes here

    int sum(int a, int b) //this is the definition of the function.
    {
    return a+b;
    }

    This way, even if the function sum() get's called before it's definition, the compiler will know where to look for it.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    yes you can make an array of function pointers...

  4. #4
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    err... I don't thing that's what he asked, ninebit.
    I could be wrong, but I got the impression he wants to create a function array, not an array of functions.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    you would create an ordinary function pointer and then point to that function pointer with an void* in the list. you must know what functions you have in advance and what params to use... if the function is called wrongly with wrong params there is unexpected behaviour... So take great care when you use this...



    it would be cool to have this for some kind of commandline type of way to run functions like this:

    cmdline> assign UP 'w'

    code:

    //parse the line "assign UP 'w'" assign - function, UP and 'w' is params

    cmd = "assign"
    paramlist("UP", "w")

    ExecuteCommand(cmd, paramlist)

    in ExecuteCommand:

    fncptr = fncMap(cmd);

    //find the case where cmd is "assign"
    if strcmp(cmd, "assign")
    fncptr(paramlist[0], paramlist[1]); // you know the assign function takes 2 params...

    anyway.. just a though and highly teorethical...

  6. #6
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    Nine-Bit's right, I want an array of functions. And I know what a prototype is

  7. #7
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    well, ok then.

    >> Is it possible to make an array of functions? eg funcarray[2]();

    You ask for an array of functions, but then give a "function array" (without better words to describe it) as an example...
    Last edited by Mario; 06-19-2002 at 07:38 PM.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Like so?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    // pointer to function typedef
    // it gets too messy to write these out in long form
    // this just happens to be what strcpy and strcat use
    typedef char * (*fnptr)(char *, const char* );
    
    // an array of pointers to functions
    fnptr functions[2] = {
        strcpy,
        strcat
    };
    
    int main ( int argc, char *argv[] ) { 
        char    test[100] = "hello";
        int     i;
    
        // calls strcpy then strcat
        for ( i = 0 ; i < 2 ; i++ ) {
            functions[i]( test, "world " );
            printf( "%s\n", test );
        }
        return 0; 
    }

  9. #9
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    You could use a token-pasting macro to achieve a similiar effect.

    ... maybe.

  10. #10
    Unregistered
    Guest
    No ,like this (this probably isn't right)
    Code:
    #include <iostream.h>
    #include <stdio.h>
    
    void myfunctions[3]();
    
    int main()
    {
    for (int i=0; i<4; i++)
    myfunctions[i];
    return 0;
    }
    
    myfunctions[0](i)
    {
    //code....
    }
    //other functions

  11. #11
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    That was me, didn't log in

  12. #12
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Use function pointers. Arrays and pointers generally are the same anyway, in a manner of speaking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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