Created
January 31, 2015 18:34
-
-
Save alexprivalov/988581741e0ba8c91059 to your computer and use it in GitHub Desktop.
Write a “C” function that modifies each element of a user supplied array by applying a user-provided function to each element of an array of integers. The user-provided function can also accept parameters. Please provide a typedef for the user-provided function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
typedef void (*func_handler)(int *p_arr_item); //typedef for user defined function | |
void func_for_processing_array(int arr[], size_t arr_size, func_handler h); | |
void do_something_with_item(int *p_item); | |
int main(int argc, char **argv) | |
{ | |
int test_arr[] = {23, 32, 43, 54, 2, 86, 90}; | |
int i = 0; | |
printf("Before processing:\n"); | |
for (; i < sizeof(test_arr)/sizeof(test_arr[0]); i++) | |
{ | |
printf("%d ", test_arr[i]); | |
} | |
printf("\n"); | |
func_for_processing_array(test_arr, sizeof(test_arr)/sizeof(test_arr[0]), do_something_with_item); | |
printf("After processing:\n"); | |
i = 0; | |
for (; i < sizeof(test_arr)/sizeof(test_arr[0]); i++) | |
{ | |
printf("%d ", test_arr[i]); | |
} | |
printf("\n"); | |
return 0; | |
} | |
void func_for_processing_array(int arr[], size_t arr_size, func_handler h) | |
{ | |
size_t i = 0; | |
for(; i < arr_size; i++) | |
{ | |
h(&arr[i]); | |
} | |
} | |
void do_something_with_item(int *p_item) //user defined function | |
{ | |
*p_item = *p_item*2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment