Originally Posted by
sakura
Yes. I'm sorry. I did include the typedef in my original code. It is not working. Please help me out here!!!
Okay. So I took the code you posted, added the typedef, added a function so that I can pass the function pointer to Init, then wrote a main function to call your Init and func functions, and it works for me:
Code:
#include <stdio.h>
typedef struct mystruct
{
int (*fp)(int, int);
} mystruct;
void Init(mystruct *handle, int (*my_fp)(int, int))
{
handle->fp = my_fp;
}
int foo(int x, int y)
{
return x + y;
}
int func(mystruct *handle)
{
int c = handle->fp(1, 2);
return c;
}
int main(void)
{
mystruct obj;
Init(&obj, foo);
printf("%d\n", func(&obj));
return 0;
}
The program compiles and prints "3".
As you can see, claiming that "somehow, it is giving me a lot of syntax errors" without also showing us the code that results in those errors and telling us what those errors are makes you look stupid when someone uses the snippets of code that you posted and proceeds to demonstrate that you are mistaken.