Last active
March 21, 2018 22:08
-
-
Save willbprog127/c01a409f0d1f4298011c222fe9d47c23 to your computer and use it in GitHub Desktop.
Function pointers in C
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
/* | |
* 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