Created
January 24, 2021 18:40
-
-
Save jstaursky/defc40cd3b4334be4658b5e7f9a956dd to your computer and use it in GitHub Desktop.
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
// 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, ¶ms}; | |
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