Re: Help needed with function pointer examples

Uli Kusterer <ulimakesacompiler@googlemail.com>
Sun, 15 Jan 2012 03:41:29 +0100

          From comp.compilers

Related articles
Help needed with function pointer examples swatirathi@cse.iitb.ac.in (Swati) (2012-01-06)
Re: Help needed with function pointer examples ulimakesacompiler@googlemail.com (Uli Kusterer) (2012-01-15)
Re: Help needed with function pointer examples gah@ugcs.caltech.edu (glen herrmannsfeldt) (2012-01-15)
Re: Help needed with function pointer examples torbenm@diku.dk (2012-01-16)
Re: Help needed with function pointer examples arnold@skeeve.com (2012-01-16)
Re: Help needed with function pointer examples DrDiettrich1@aol.com (Hans-Peter Diettrich) (2012-01-17)
Re: Help needed with function pointer examples dot@dotat.at (Tony Finch) (2012-01-19)
| List of all articles for this month |

From: Uli Kusterer <ulimakesacompiler@googlemail.com>
Newsgroups: comp.compilers
Date: Sun, 15 Jan 2012 03:41:29 +0100
Organization: Compilers Central
References: 12-01-006
Keywords: analysis
Posted-Date: 14 Jan 2012 23:32:27 EST

On 06.01.2012, at 07:43, Swati wrote:
> We are doing an analysis on the usage of function pointers.
> We have considered one such example as GCC which uses function pointers.
> Can you suggest any other application which uses function pointers
> extensively.




  That is a vague, underspecified question if I ever heard one. What
exactly are you looking for with function pointers? Have you
implemented them in your compiler and want some examples to see if
your compiler implements everything necessary?


  Anyway, the first example that comes to mind is any application that
can dynamically trigger actions, or that has to translate files or
data into actions. E.g. many interpreters and emulators essentially
look like this (untested C code fragment, never compiled):


struct Instruction { int instructionIndex; int param1; int param2 };
typedef void (*InstructionFuncPtr)( Instruction inInstruction );


InstructionFuncPtr gInstructions[2] = { AddInstruction, SubtractInstruction
};


void main(void)
{
Instruction vInstructions[10];


// ... read instructions from stdin or a file into 'vInstructions' ...


for( int x = 0; vInstructions[x] != -1; x++ )
gInstructions[ vInstructions[x].instructionIndex ]( vInstructions[x] );


return 0;
}


Where AddInstruction and SubtractInstruction are two functions that actually
implement the given operations.


Cheers,
-- Uli Kusterer
http://stacksmith.com


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.