What Brafil said, plus:
Code:
cpush(&data, &printyay, sizeof(fp));
This doesn't do what you think it does. &printyay is exactly the same as printyay (without the &) in this context.
In other words, you are passing a pointer to a function when instead you should be passing a pointer to a pointer to a function.
[edit]
Try this:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct cstack {
void** stack;
int items;
} cstack;
void cpush(cstack* stackp, void* data, int size)
{
stackp->items++;
stackp->stack=realloc(stackp->stack, stackp->items * sizeof(void*));
stackp->stack[stackp->items-1] = malloc(size);
memcpy((stackp->stack)[stackp->items-1], data, size);
}
void* cpop(cstack* stackp)
{
void* data=NULL;
stackp->items--;
data=(stackp->stack)[stackp->items];
if(stackp->items==0)
{
free(stackp->stack);
stackp->stack=NULL;
}
else
stackp->stack=realloc(stackp->stack, stackp->items * sizeof(void*));
return data;
}
void printyay()
{
printf("Yay\n");
}
int main()
{
cstack *data=malloc(sizeof(cstack));
typedef void (*fp)();
fp *callme=NULL;
fp yayfunc = printyay;
char buf[50], *message=NULL;
int num=27,*newnum=NULL;
sprintf(buf, "Hello there.\n");
/* Push data into stack */
cpush(data, &yayfunc, sizeof(fp));
cpush(data, &num, sizeof(int));
cpush(data, &buf, sizeof(char)* 50);
/* Pop from data */
message=(char*)cpop(data);
newnum=(int*)cpop(data);
callme=(fp*)cpop(data);
printf("%s%d\n", message, *newnum);
(*callme)();
free(callme);
free(message);
free(newnum);
free(data);
return 0;
}
[/edit]