Reputation: 526
I have a simple question:
Let's supose that I have a function called _foo
.
void _foo (void *, void *, int);
Now, I want to create a new struct
that holds a ponter to _foo
, so:
struct _st {
struct _st *next;
void (*action)(void *, void *, int);
};
And then I initialize one struct _st
variable:
struct _st test = {0x0, _foo};
My question is: is there a way that I can set test.action
whithout being by the name of the function? I mean, a fixed address or something like?
Upvotes: 1
Views: 70
Reputation: 6116
test.action
is a pointer you can set it to any address (numeric address e.g. 0x2000
) you want, _foo
is also a symbolic name for an address. However for doing that you need to keep in mind the following points:
You should typecast it properly to suppress compiler warning/error.
Be cautious about the undefined behavior you may invoke by accessing some address not accessible to you including dreaded SEGFAULT runtime error.
It is always sensible to use symbolic names/constants for such assignments to help you understand your own code in future as why did you assigned it, how can you easily modify it, etc.
Upvotes: 3
Reputation: 16407
You could use an address only if you set that address through the linker. Otherwise, the name is pretty much the only reference you have to that function.
Upvotes: 2