0

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);
  • Its called pointer dereferencing - this question might help http://stackoverflow.com/questions/4955198/what-does-dereferencing-a-pointer-mean – mathematician1975 Nov 17 '13 at 21:07

4 Answers4

2

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.

Thanatos
  • 42,585
  • 14
  • 91
  • 146
  • oh so arg is basically storing the address that the int * was pointing to or the value when its dereferenced ? – jeniffer_214 Nov 17 '13 at 21:16
  • We can't see the type of arg, so it's hard to say exactly what it is storing. Since we're casting it to an `int *`, hopefully it's a pointer type (and thus storing an address). Typically, the reason to perform that cast is because `arg` is a `void *`, and you have some knowledge that it points to an integer. (And is thus safe to cast.) – Thanatos Nov 17 '13 at 23:29
1

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

Charlie Burns
  • 6,994
  • 20
  • 29
0

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;
mrks
  • 8,033
  • 1
  • 33
  • 62
0

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.

godel9
  • 7,340
  • 1
  • 33
  • 53