Instantly share code, notes, and snippets.

Embed
What would you like to do?
apply() Demo
#include <stdio.h>
extern void* apply(void* func, void** args, size_t argc);
long bar(int a, int b, long c, long d, int e, long f, long g, int h) {
printf( "a = %x\n"
"b = %x\n"
"c = %lx\n"
"d = %lx\n"
"e = %x\n"
"f = %lx\n"
"g = %lx\n"
"h = %x\n",
a, b, c, d, e, f, g, h);
return h;
}
long foo(long a, long b, long c) {
long baz = a ^ b & c;
printf("a ^ b & c = %d\n", baz);
long args[] = {
0xaaddaadd,
0xddaaddaa,
0xaaaaaaaabbbbbbbb,
0x1234123412341234,
1,
0x4321432143214321,
0x9879879879879879,
2
};
void* result = apply(bar, (void**) &args, 8);
printf("--> %ld\n", (long) result);
return (long) result;
}
int main() {
long args[] = {3, 4, 5};
void* result = apply(foo, (void**) &args, 3);
printf("-> %ld\n", (long) result);
return 0;
}
/*
$ ./test
a ^ b & c = 7
a = aaddaadd
b = ddaaddaa
c = aaaaaaaabbbbbbbb
d = 1234123412341234
e = 1
f = 4321432143214321
g = 9879879879879879
h = 2
--> 2
-> 2
*/
@vseloved

This comment has been minimized.

vseloved commented Jul 15, 2012

What about arguments of different types?

@vedantk

This comment has been minimized.

Owner

vedantk commented Jul 19, 2012

This implementation doesn't handle types larger than 8 bytes. You might be interested in libapply (https://github.com/davvid/libapply), which is a much more general implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment