Created
October 25, 2013 14:59
-
-
Save semahawk/7156043 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 defines two functions (excluding 'main') and makes a random call | |
* to one of those. | |
* | |
* Just wandering if C supports this kind of 'feature'. | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <time.h> | |
void first(void) | |
{ | |
printf("first called\n"); | |
} | |
void second(void) | |
{ | |
printf("second called\n"); | |
} | |
void (*funcs[])(void) = { first, second }; | |
int main(void) | |
{ | |
unsigned r; | |
srand(time(NULL)); | |
r = rand() % 2; | |
printf("calling %p (%p, %p)\n", (void *)funcs[r], (void *)first, (void *)second); | |
funcs[r](); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment