Skip to content

Instantly share code, notes, and snippets.

@jstaursky
Created January 24, 2021 18:40
Show Gist options
  • Save jstaursky/defc40cd3b4334be4658b5e7f9a956dd to your computer and use it in GitHub Desktop.
Save jstaursky/defc40cd3b4334be4658b5e7f9a956dd to your computer and use it in GitHub Desktop.
// Credit: https://stackoverflow.com/questions/189725/what-is-a-trampoline-function
#include <stdio.h>
typedef struct _trampoline_data {
void(*callback)(struct _trampoline_data*);
void* parameters;
} trampoline_data;
void trampoline(trampoline_data* data) {
while(data->callback != NULL)
data->callback(data);
}
//-----------------------------------------
typedef struct _factorialParameters {
int n;
int product;
} factorialParameters;
void factorial(trampoline_data* data) {
factorialParameters* parameters = (factorialParameters*) data->parameters;
if (parameters->n <= 1) {
data->callback = NULL;
}
else {
parameters->product *= parameters->n;
parameters->n--;
}
}
int main() {
factorialParameters params = {5, 1};
trampoline_data t = {&factorial, &params};
trampoline(&t);
printf("\n%d\n", params.product);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment