Skip to content

Instantly share code, notes, and snippets.

@gabtoschi
Created November 28, 2020 00:51
Show Gist options
  • Save gabtoschi/53423d7bb72ec4cf0f0ae2cac5b6a2af to your computer and use it in GitHub Desktop.
Save gabtoschi/53423d7bb72ec4cf0f0ae2cac5b6a2af to your computer and use it in GitHub Desktop.
Some function pointer examples in C
#include <stdlib.h>
#include <stdio.h>
void fullMessage(char *name, void (*greeting)(char *)) {
greeting(name);
printf("Good luck in your life!\n");
}
void hello(char *name) {
printf("Hello, %s! ", name);
}
void goodbye(char *name) {
printf("Goodbye, %s! ", name);
}
int main(int argc, char *argv[]){
char *name = argv[1];
fullMessage(name, hello);
fullMessage(name, goodbye);
fullMessage(name, name[0] % 2 == 0 ? hello : goodbye);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment