Sunday, September 8, 2013

Dereferencing struct void pointer in C

Dereferencing struct void pointer in C

I have a MyLib.h which has
typedef void *MyThread;
and a MyLib.c which has:
typedef struct
{
ucontext_t *context;
int tid;
}_MyThread;
there is a test function that creates a thread and issues a join as:
void f0(void * dummy)
{
MyThread t1;
printf("f0 start\n");
t1 = MyThreadCreate(f1, NULL);
_MyThread *t = (_MyThread*)t1;
printf("passed value=%d\n",t->tid);
printf("f0 join on t1\n");
MyThreadJoin(t1);
....
}
MyThreadJoin in MyLib.c does:
int MyThreadJoin(MyThread thread)
{
_MyThread *arg_thread;
arg_thread = (_MyThread*)thread;
int id = arg_thread->tid;
....
}
My problem is, when I run the above, I get:
passed value=2
f0 join on t1
arg_thread->tid = 13
Why is the passed value dereferenced in the same way coming different from
calling function and different in called function?

No comments:

Post a Comment