So i thought this would be straight forward but i keep getting compile errors, which probably means i am doing something stupid.
So i am using C and i want to save a pointer to a function in a global variable.
As a test case i have three files. Test.c, Test.h and FunctionPtr.c
Test.h goes like this
void (*MyCallBack)(void);
int SetFunc( void (*CallBackArg)(void));
void CallFunc();
Test.c Goes like this
#include <stdio.h>
#include <stdlib.h>
#include "test.h"
void SetFunc( void (*CallBackArg)(void)){
printf("Set CallBack\n");
MyCallBack=CallBackArg;
}
void CallFunc(){
printf("In Call Func\n");
MyCallBack();
}
and FunctionTest.c goes like this
#include <stdio.h>
#include <stdlib.h>
#include "test.h"
void MyFunc(){
printf("Work!\n");
}
int main()
{
SetFunc(MyFunc);
CallFunc();
}
When i compile using gcc FunctionTest.c test.c test.h -o FunctionTest
I get the following error…
test.c:4: error: conflicting types for ‘SetFunc’
test.h:4: note: previous declaration of ‘SetFunc’ was here
I cant figure out what i am doing wrong? Perhaps i am not declaring the Global Pointer right?
The return types are different, that’s your problem. You have to declare it the same way in the header as in the .c file.