Skip to content

Instantly share code, notes, and snippets.

@pkulev
Created March 19, 2015 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkulev/2e001cbe6aa9bc51c4e0 to your computer and use it in GitHub Desktop.
Save pkulev/2e001cbe6aa9bc51c4e0 to your computer and use it in GitHub Desktop.
#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