Skip to content

Instantly share code, notes, and snippets.

@jstaursky
Created June 26, 2019 15:48
Show Gist options
  • Save jstaursky/1b41e394c60a3c3f1d8a98400fb2511b to your computer and use it in GitHub Desktop.
Save jstaursky/1b41e394c60a3c3f1d8a98400fb2511b to your computer and use it in GitHub Desktop.
#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