Skip to content

Instantly share code, notes, and snippets.

@semahawk
Created October 25, 2013 14:59
Show Gist options
  • Save semahawk/7156043 to your computer and use it in GitHub Desktop.
Save semahawk/7156043 to your computer and use it in GitHub Desktop.
/*
* 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