Pointer problem in c.
Im writing some stuff in c, and I''m using a void * pointer to hold various pieces of data. Yay. Cool. Fun. Piece of cake.
If you dont understand what I''m talking about now please dont answer this thread.
I''m considering using the pointers to also hold the addresses of my functions. I know I _should_ be able to do that, but Im not quite sure how.
Can some c guru help me out? While I know how to cast my function to void pointer, I dont know how to cast it back to a function to use.
Ie. int junkOp (int x);
int (* objpt) (int x);
objpt = (* junkOp);
x = (* objpt) (y);
Works.
Now, I have:
void *data;
data = (void *) (* junkOp);
Now I want something like:
objpt = (int (int x)) data;
But the compiler argues:
linked_list.c: In function `main'':
linked_list.c:34: cast specifies function type
...well. Yes. Thats what I want to do. I cant figure out why it wont let me; except my cast to the function must be incorrectly formatted. Anyone able to help out on this?
I cant find anything useful anywhere on it.
...and if you can: let me know how you''d do a cast to a function of type int *junkOp2 (int *x) as well!
Thanks!
One way is
int junkOp(int x);
typedef int (*OBJPT)(int);
OBJPT myPt;
void *data = junkOp;
myPt = (OBJPT)data;
int junkOp(int x);
typedef int (*OBJPT)(int);
OBJPT myPt;
void *data = junkOp;
myPt = (OBJPT)data;
A polar bear is a rectangular bear after a coordinate transform.
Hi !
This maybe not your thing, but I''m always using inline-assembly
to do stuff like this:
long MyFunc (void *vPtr, long lVal1, long lVal2);
void *fnFunc;
void *vPtr_Par;
long lVal1_Par,
lVar2_Par;
:
:
:
fnFunc = (void*)MyFunc;
_asm {
pushad
push lVal2
push lVal1
push vPtr
call fnFunc
mov lReturn, eax
add esp, 12 // 3*4 long vals
popad
}
=> You have to compile this with fast-call CallingConvention...
Roman
This maybe not your thing, but I''m always using inline-assembly
to do stuff like this:
long MyFunc (void *vPtr, long lVal1, long lVal2);
void *fnFunc;
void *vPtr_Par;
long lVal1_Par,
lVar2_Par;
:
:
:
fnFunc = (void*)MyFunc;
_asm {
pushad
push lVal2
push lVal1
push vPtr
call fnFunc
mov lReturn, eax
add esp, 12 // 3*4 long vals
popad
}
=> You have to compile this with fast-call CallingConvention...
Roman
- www.sunage-the-game.com - - www.vertex4.com - Sauk says, 'Loading is fun...'
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement