i understand this being pointer *(); but *((type *)type); is whats confusing me
I have tried to understand pointers but its confusing me.
int offset = *((int *)arg);
i understand this being pointer *(); but *((type *)type); is whats confusing me
I have tried to understand pointers but its confusing me.
int offset = *((int *)arg);
Let's break this down:
(int *)arg;
This takes arg
and casts it to an int *
. The result of this expression is, of course, an int *
.
*((int *)arg);
We're now just dereferencing that int *
we just came up with — the result is the integer that the pointer was pointing to.
int offset = *((int *)arg);
We assign that integer into offeset.
No multiplication is involved here.
This (int *)arg
takes whatever type arg
is and pretends it is a int *
This *((int *)arg)
takes the above pointer and deferences it, returning an int
You are casting arg
to a pointer to int which is then dereferenced.
Think of it this way:
void* arg;
int* ptr = (int*) arg;
int offset = *ptr;
It's a cast followed by a dereference. Basically, you're telling the compiler, "Trust me. I know that arg
points to an int
, so read the int
it's pointing to, and put it into offset
.