{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

An Array Mapper

An Array Mapper - An Array Mapper typedef void*FuncPtr(int...

Info icon This preview shows pages 1–7. 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 ) { printf("val = %d \n", val);
Image of page 1

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

View Full Document Right 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); }
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); } }
Image of page 3

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

View Full Document Right 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); }
Image of page 4
Using the Generic Mapper int a[ ] = {3,4,7,8}; main( ) { // Print integer values s = 0; genericArrayMapper( a, sizeof(a)/sizeof(int), sizeof(int), printIntGen); // Compute sum the integer values
Image of page 5

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

View Full Document Right Arrow Icon
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern