Created
June 26, 2019 15:48
-
-
Save jstaursky/1b41e394c60a3c3f1d8a98400fb2511b to your computer and use it in GitHub Desktop.
This file contains 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
#include <stdio.h> | |
struct fn_struct { | |
int number; | |
void (*f) (void *); | |
}; | |
void foo (unsigned int m) | |
{ | |
unsigned int tmp = 0; | |
while (tmp++ < m) | |
puts ("foo function"); | |
} | |
void bar (char *words) | |
{ | |
puts (words); | |
} | |
int main (int argc, char *argv[]) | |
{ | |
// Note compound literals have auto storage duration. | |
struct fn_struct *fn_ptr = | |
&(struct fn_struct){ .number = 1, | |
.f = (void (*) (void *))bar }; | |
(*(void (*) (char *))fn_ptr->f) ("hello world"); | |
fn_ptr->f = (void (*) (void *))foo; | |
(*(void (*) (unsigned int))fn_ptr->f) (3); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment