Skip to content

Instantly share code, notes, and snippets.

@Tacumi
Created July 18, 2018 04:48
Show Gist options
  • Save Tacumi/83211726b8de5ca6ca2697ebc5061365 to your computer and use it in GitHub Desktop.
Save Tacumi/83211726b8de5ca6ca2697ebc5061365 to your computer and use it in GitHub Desktop.
Apply function in C
#include <stdio.h>
#include <stdlib.h>
int multiply10 (int in)
{
return in * 10;
}
void apply (int (*fpa)(int), int *pa, size_t siz)
{
for (int i = 0; i < siz; i++)
{
*(pa + i) = (*fpa)(*(pa + i));
}
}
int main()
{
int *pa = malloc(sizeof(int)*10);
size_t arraysize = 10;
/* 初期化 */
for (int i = 0; i < arraysize; i++)
{
pa[i] = i + 1;
}
/* 初期状態を表示 */
for (int i = 0; i < arraysize; i++)
{
printf("%d ", *(pa + i));
}
printf("\n");
apply (multiply10, pa, arraysize);
/* 適用後の状態を表示 */
for (int i = 0; i < arraysize; i++)
{
printf("%d ", *(pa + i));
}
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment