An Array Mapper

An Array Mapper - An Array Mapper typedef void...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
An Array Mapper typedef void (*FuncPtr)(int a); void intArrayMapper( int *array, int n, FuncPtr func ) { for( int = 0; i < n; i++ ) { (*func)( array[ i ] ); } } int s = 0; void sumInt( int val ){ s += val; } void printInt( int val ) {
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Using the Array Mapper int a[ ] = {3,4,7,8}; main( ){ // Print the values in the array intArrayMapper(a, sizeof(a)/sizeof(int), printInt); // Print the sum of the elements in the array s = 0; intArrayMapper(a, sizeof(a)/sizeof(int), sumInt); printf(“total=%d\”, s); }
Background image of page 2
A More Generic Mapper typedef void (*GenFuncPtr)(void * a); void genericArrayMapper( void *array, int n, int entrySize, GenFuncPtr fun ) { for( int i = 0; i < n; i++; ){ void *entry = (void*)( (char*)array + i*entrySize ); (*fun)(entry); } }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Using the Generic Mapper void sumIntGen( void *pVal ){ //pVal is pointing to an int //Get the int val int *pInt = (int*)pVal; s += *pInt; } void printIntGen( void *pVal ){ int *pInt = (int*)pVal; printf("Val = %d \n", *pInt); }
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/30/2012 for the course CS 252 taught by Professor Gustavorodriguez during the Fall '11 term at Purdue University.

Page1 / 7

An Array Mapper - An Array Mapper typedef void...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online