delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/21/10:15:16

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Array of Functions
Date: Wed, 16 Jul 1997 20:45:49 +0000
Organization: Two pounds of chaos and a pinch of salt
Lines: 48
Message-ID: <33CD32FD.3A6@cs.com>
References: <19970715 DOT 215516 DOT 5183 DOT 0 DOT chambers DOT ben AT juno DOT com>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp107.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Benjamin D Chambers wrote:
> 
> Recently (today), I tried to set up an array of functions to process data
> (This way, I can just call a function from an array, using the data type
> ID as an index into the array, and have it processed appropriately).
> Unfortunatley, djgpp doesn't seem to like this.  Specifically, it gave me
> the error:
> fe.c(10): declaration of 'Element_Functions' as array of functions,
> 
> and died on me.  Does anyone have thoughts about how to solve this?
> Thanx in advance,

Hmm... we can't tell what your problem is without a specific code
sample.  But if I were to declare an array of functions, this is how I'd
do it:

typedef int (*ARRAY_FUN) ( DATA data, int foo );

ARRAY_FUN function_list[] =
{
    handle_zorks,
    handle_erks,
    handle_dits,
    handle_dats,
    NULL
};

Of course, all the above functions must be in scope at the time the
array is initialized, and must all match the above typedef.  You can
skip the typedef step (although it makes things harder) by doing the
following:

int (*(function_list[])) ( DATA data, int foo ) =
{
    /* ... */
};

To access the array from a function, follow this example:

    (*function_list[i]) ( data, foo );

-- 
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I      |   mailto:fighteer AT cs DOT com   |
| God's final message to His Creation: | http://www.cs.com/fighteer |
| "We apologize for the inconvenience."| <<< This tagline under >>> |
|                      - Douglas Adams | <<<    construction    >>> |
---------------------------------------------------------------------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019