javascript - Change a function dynamically in C -
i know if there equivalent in c (for void
function) javascript code:
var myfunction; myfunction = function(){ //some code }
not equivalent (because c static language without support anonymous or nested functions) can have variable pointer function, , make point different compiled functions matching type of variable.
very simple , basic example:
#include <stdio.h> void function1(void) { printf("function1\n"); } void function2(void) { printf("function2\n"); } int main(void) { // declare variable pointer function taking no arguments // , returning nothing void (*ptr_to_fun)(void); ptr_to_fun = &function2; ptr_to_fun(); ptr_to_fun = &function1; ptr_to_fun(); return 0; }
the above program print out
function2 function1
Comments
Post a Comment