You can use functions pointers to store references to functions, which will work across code spaces (files, libraries etc). The fuctions pointers can be stored in variables, structures, tables etc. Its just regular pointer type (unsigned int).
On embedded systems you can be fooled by endianess if you cast the int to unsigned chars (as with any other pointer), so be aware. Unlike some other poster in this thread, I do not agree that this is depended os hw architecture or os. This is native C functionallity a can be used on any platform that has native C support, which is basically all.
For declaring a function pointer
Code:
typedef funcptr_t int (*func)(int a, int b);
then you have the type funcptr which you can use, for example in a struct
Code:
typedef struct {
int a; // Just an integer
funcptr_t someFunc;
}funcPtrContainer_t, *funcPtrContainer_t;
The you declear a function to use
Code:
int someFuncThatDoSomething(int oneVar, int twoVar)
{
// Function body
return 0;
}
In the program you just, declear the following;
Code:
funcptr_t funcPtr = NULL;
funcPtrContainer_t funcPtrCont;
funcPtr = someFuncThatDoSomething;
funcPtrCont.someFunc = someFuncThatDoSomething;
Then you call them some where in the code, in either way:
Code:
int result = 0;
result = funcPtr(1, 4);
result = (*funcPtr)(1, 4);
result = funcPtrCont.someFunc(1, 4);
result = (*funcPtrCont.someFunc)(1, 4);
Which all yield the same result.
Function pointer are perticulary useful in real applications where runtime binding is needed, an is basically the early idea behind ploymorphism in OO languages.
G