From: Jeramie DOT Hicks AT mail DOT utexas DOT edu (Jeramie Hicks) Newsgroups: comp.os.msdos.djgpp Subject: Re: Pointers to functions. Date: Mon, 08 Mar 1999 15:36:09 GMT Organization: The University of Texas at Austin, Austin, Texas Lines: 27 Message-ID: <36e3ec89.57180317@newshost.cc.utexas.edu> References: <7b9jku$o7o$1 AT news6 DOT svr DOT pol DOT co DOT uk> <36D9BD2F DOT A05C8E55 AT cartsys DOT com> <7br6n8$897$1 AT news6 DOT svr DOT pol DOT co DOT uk> <7bu8hk$6b6$1 AT camel19 DOT mindspring DOT com> NNTP-Posting-Host: dial-109-4.ots.utexas.edu X-Newsreader: Forte Free Agent 1.11/32.235 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com On Sun, 7 Mar 1999 18:18:30 +0000, Dave Bird wrote: > You might want to write a clever sort algorithm which can sort many > different kinds of data. That's a good example. Here's another: Suppose you want to choose one function out of a hundred to handle a specific chunk of data, like an opcode handler for a emulator. Instead of having a switch statement that's hundreds of lines long, as in: switch(opcode) { case OPCODE1: OpcodeHandler1(); case OPCODE2: OpcodeHandler2(); ... case OPCODE254: OpcodeHandler254(); case OPCODE255: OpcodeHandler255(); }; You can have a clean, fast array of function pointers that is initalized at the start. Then, when OPCODEX is read, the function at the array index X is executed. Clean, fast, efficient. If you replace the unused functions with a pointer to a default handler, it's even better. - Hicks