Created
November 28, 2020 00:51
-
-
Save gabtoschi/53423d7bb72ec4cf0f0ae2cac5b6a2af to your computer and use it in GitHub Desktop.
Some function pointer examples 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
#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