Created
March 19, 2015 12:09
-
-
Save pkulev/2e001cbe6aa9bc51c4e0 to your computer and use it in GitHub Desktop.
This file contains 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> | |
#include <stdlib.h> | |
int pow2(int val) | |
{ | |
return val * val; | |
} | |
int* map(int (*func)(int), int *arr, int len) | |
{ int *res = malloc(sizeof(int) * len); | |
for (int i = 0; i < len; i++) { | |
res[i] = func(arr[i]); | |
} | |
return res; | |
} | |
int main(int argc, char **argv) | |
{ | |
int len = 10; | |
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; | |
int *powered2 = map(pow2, arr, len); | |
for (int i = 0; i < len; i++) { | |
printf("original: %d, powered: %d\n", arr[i], powered2[i]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment