There are no easy way to do that in C. Of course, you could write assembler code to dynamically create a function with specified argument (assuming you are allowed to write such code in your developing environment).
On the other hand, you probably don't really need that. Usually, you would have a static function taking instance argument in your struct and that function would call member function of that instance.
It is not clear if you are writing C or C++ code. If you write C code, then you don't have member function anyway so it is pointless to do that kind of stuff. If you write C++ code, then you should have used new instead of malloc anyway...
Also, it look weird that you want to set a function pointer in a struct point to a global function.
Obviously, it that is what you want to do, then it is extremely simple (assuming C++):
struct Foo
{
void(*Print)(void* Instance);
void PrintMe() { (*Print)(this); }
};
If you really want to it the other way, then you would have a static member taking a parameter that would be interpreted as an instance:
struct Foo2
{
static void PrintFuction(void *instance)
{
reinterpret_cast<foo2>(instance)->PrintMember();
}
static void PrintMember() { }
};
By the way, in other language as C#, the compiler would essentially generate a class with the data in the members of it. And such function would be represented by a pair of {function, instance}.
Also, in modern C++ you also have such function object.
Thus it is not clear what you are trying to achieve with that and it might not be the best way to do it... in particular if you have access to the whole code.
— Update (2016-10-07)—
As indicated in comments, it is not possible to achieve desired syntax using only standard C.
For "Memory access callback", I didn't know about that. I have found the following information:
http://stackoverflow.com/questions/6211429/callback-on-memory-access/6211502[
^].
The thing I can say it that it is a specific Linux function. And to use it for the purpose of simplifying the syntax, one would have to ensure that he use the proper calling convention, that any used compiler optimization are compatible with the hack and that he would never want to port the code to any other platform where such hack might not be possible.
Essentially, the hack would be that in some specific cases, you might know in which register or relative stack location is the address of the function just called. From that information, you might be able to compute the address of "this" and make any necessary adjustment so that the function see the correct information.
And even then, that hacking might give you problems with debugging, error handling or compatibility issues.
This is not something that should be used for regular development or just for the convenience of a nicer syntax. If this is what you want, then you should seriously consider a C++ compiler.
That kind of hack might be useful if one develop some specific library for a specific platform for things like RTOS, profilers, stack trace analysers... But if you wrote such tools, then you need to have a deep understanding of your platform and compiler.
Seriously... uses a C++ compiler!
It does not make sense to do that kind of hack in most cases and in particular if the only purpose is to have a nicer syntax.
Obviously, there are so many reason why it might break that it won't worth the effort. In fact, if you application crash or does not behave correctly afterwards, you have no idea if the problem is caused by the hack and no easy way to test it since you would have to revert all the changes you made to improve the code.
Alternative
You can use a C++ compiler that is able to generate C code. See
http://stackoverflow.com/questions/3311563/do-all-c-compilers-generate-c-code[
^] for more information.