Skip to content

Instantly share code, notes, and snippets.

@willbprog127
Last active March 21, 2018 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willbprog127/c01a409f0d1f4298011c222fe9d47c23 to your computer and use it in GitHub Desktop.
Save willbprog127/c01a409f0d1f4298011c222fe9d47c23 to your computer and use it in GitHub Desktop.
Function pointers in C
/*
* this program demonstrates function pointers in C
*/
#include <stdlib.h>
#include <stdio.h>
/* define our 'object' struct */
typedef struct {
void (*my_cute_function)(void);
} MyObject;
/* function that returns 'new' type 'object' */
MyObject * new_object ()
{
return malloc(sizeof(MyObject));
}
/* function we're pointing to */
void yeah ()
{
printf("Test\n");
}
/* initialize the function pointer */
void object_init (MyObject * o)
{
o->my_cute_function = yeah;
}
/* now test it out! */
int main (int argc, char** argv)
{
MyObject * obj = new_object();
object_init(obj);
obj->my_cute_function();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment