Talha A. B.
2010-09-03 13:19:16 UTC
#include
#include
#include
struct myStruct
{
char message[10];
int (*hello)(struct myStruct *self);
};
int hello(struct myStruct *self)
{
printf("%s\n", self->message);
return 0;
}
int main()
{
struct myStruct *frodo;
printf("START\n");
frodo = malloc(sizeof(*frodo));
frodo->hello = hello;
strcpy(frodo->message, "printing message");
/* Line A */ frodo->hello(frodo);
/* Line B */ hello(frodo);
free(frodo);
printf("END\n");
return 0;
}
Now the line marked Line A is the problem. For some reason the program always segfaults at that point, even though Line B works just fine. Also, this problem only occurs when i use dynamic memory allocation. If I declare a normal myStruct variable, and assign the values to it, it works just fine. Any ideas what I'm doing wrong?