Array of Functions 
Author Message
 Array of Functions

Hi,

Does anybody out there know what's array of functions. I checked out
in some books but can not find anything on this. Is this generic to C
or is it a trick discovered by some programmers...???

e.g.

unsigned  int ( *FuncArray[] ) ( ) = {  func1, func2, func3 }

Thanks & Regards,
Edmund



Sat, 17 Apr 1999 03:00:00 GMT  
 Array of Functions

Array of functions is just a programming technique.
It is just array of entry points and commonly used in assembly.
I used to have arrary of functions in assembly (then C later) for almost
all of my large projects(over million lines of code).
I don't find many programmers like to see it.

--

Bugs in my code says I am not perfect.




Quote:
> Hi,

> Does anybody out there know what's array of functions. I checked out
> in some books but can not find anything on this. Is this generic to C
> or is it a trick discovered by some programmers...???

> e.g.

> unsigned  int ( *FuncArray[] ) ( ) = {  func1, func2, func3 }

> Thanks & Regards,
> Edmund



Sat, 17 Apr 1999 03:00:00 GMT  
 Array of Functions

Subject: SUMMARY: Function Pointer Question
Newsgroups: comp.lang.c

REPLY 4:


/* zeiger.c, demonstrationsmodule about the */
/* use of pointers to functions             */

#include <stdio.h>

/* prototypes */
void funktion1( void );
void funktion2( void );

struct { int i; void (*funk)( void ); } structure[] = {
                                        {1, funktion1},
                                        {2, funktion2} };
int main( void )
{
 structure[0].funk();           /* funktion1 called via pointer  */
                   /* ANSI-C                        */

 (*structure[1].funk)();        /* funktion2 called via pointer  */
                   /* ANSI-C und K&R-C              */

 structure[0].funk=funktion2; /* now we call funktion2 with the structure[0] */

 structure[0].funk(); /* and call it */

Quote:
}

void funktion1( void )
{
 printf("Funktion 1 called\n");

Quote:
}

void funktion2( void )
{
 printf("Funktion 2 called\n");

Quote:
}

h.f.s.
--
Hans Friedrich Steffani
Institut fuer Elektrische Maschinen und Antriebe, TU Chemnitz-Zwickau

http://www.tu-chemnitz.de/~hfst/


Sat, 17 Apr 1999 03:00:00 GMT  
 Array of Functions

:
: Hi,
:
: Does anybody out there know what's array of functions. I checked out
: in some books but can not find anything on this. Is this generic to C
: or is it a trick discovered by some programmers...???

I've never seen it myself.

:
: e.g.
:
: unsigned  int ( *FuncArray[] ) ( ) = {  func1, func2, func3 }

Hey, this is an array of *pointers* to functions ([] have a greater
"precedence" than *). The initialiser is good because func1 "decays" into
the equivalent of &func1.

Array of pointers to functions are usually used to implement run-time
binding (like in object-oriented programs), i.e. selection of a function
to execute based on informations only available at run-time. Another use
is in implementation of finite states machine (as opposed to big
switches).

A real array of functions would have a syntax similar to:

unsigned  int ( FuncArray[] ) ();

and this is refused by my compiler (well, note that it can be wrong).

Hope it helps,

Antoine



Sat, 17 Apr 1999 03:00:00 GMT  
 Array of Functions


Quote:

>Does anybody out there know what's array of functions. I checked out
>in some books but can not find anything on this. Is this generic to C
>or is it a trick discovered by some programmers...???

I've used this quite a bit recently, and it's actually quite useful in
the right context.  I'm processing a bunch of data files, where each file
conforms to one of several different formats.  The type of the file is
indicated by the file name.  Rather than have a block of code that reads

    if (STR_IPatMatch (Filename, "*.dat") == TRUE)
        /* process data file */
    else if (STR_IPatMatch (Filename, "*.ctl") == TRUE)
        /* process control file */
    else if (STR_IPatMatch (Filename, "*.cal") == TRUE)
        /* process calibration file */
    else
        /* unknown file type */

I have an array of structs with the pattern to match against and a
pointer to the function to call,  like so:

    typedef struct {
        char pattern[PATTLEN];
        void (*parser) ();
    } FTableRec;

    FTableRec FTable[] = {
        {"*.dat", ParseDataFile},
        {"*.ctl", ParseControlFile},
        {"*.cal", ParseCalibrationFile},
        {"*", ParseUnknownFile}
    };

    int FTableEntries = (int) sizeof (FTable) / (int) sizeof (FTableRec);
    ...
    i = 0;
    while (i < FTableEntries)
    {
        if (STR_IPatMatch (Filename, FTable[i].pattern) == TRUE)
        {
            (*FTable[i].parser) (Filename);
            break;
        }
        i++;
    }

The biggest disadvantage to this scheme is that the syntax is kind of
gross, and you may find yourself having to explain what it is you are
doing to your colleagues, but the payoff comes in having to only maintain
the table, instead of having to add a new condition to what could be a
long list of conditions (the actual code I'm working on handles something
like 17 different file types).  

jfb



Sun, 18 Apr 1999 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Passing 2D arrays to functions with const.

2. array as function arg

3. Array of Function Calls?

4. cast char array to function pointer?

5. Passing Arrays to functions

6. 3D Arrays and Functions

7. passing arrays to functions & other stuff

8. How can i change a array in function (without creating a new one)

9. Passing 2D arrays to function

10. Passing Arrays to functions

11. Passing 2D Arrays to functions

12. passing dynamically allocated arrays to functions

 

 
Powered by phpBB® Forum Software