Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Greenspun's Tenth Rule

Name: Anonymous 2014-04-25 11:23


#include <stdio.h>
#include <stdlib.h>

void *apply(void *callee, int argc, void *argv) {
void **as = (void**)argv;
void *(*f)(void *a,...) = (void *(*)(void *a, ...))callee;

switch(argc) {
case 0: return ((void* (*)())f)();
case 1: return f(as[0]);
case 2: return f(as[0], as[1]);
case 3: return f(as[0], as[1], as[2]);
case 4: return f(as[0], as[1], as[2], as[3]);
default:
printf("apply: number of arguments limit reached\n");
abort();
}
}

int main(int argc, char **argv) {
char *as[] = {"hello %s\n" , "world"};
apply(printf, 2, as);
return 0;
}

Name: Anonymous 2014-04-26 1:17

>>12
POSIX requires that this be possible (e.g. for dlsym) even though DA STANDARD itself does not. So in practice you can assume this will work.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List