double casting in c

c, casting

Solution

It looks like this code is using several `unsigned short int`s to store pointers in two halves. This assumes that an `unsigned short int` is half as wide as a pointer. The code is extracting the stored pointer from two adjacent shorts, namely the second and third member of an array, and interpreting it as a pointer to an `unsigned long long int`.

So, the final cast is necessary to reinterpret an integer as a pointer, while the first (inner) cast serves to read a differently typed value from an existing variable, namely a long from a short (or rather, from two adjacent shorts).

Problem

I have gone over this code and I have noticed this casting that looks weird ``` oldidt = (unsigned long long *)(*(unsigned long*)(oldidtr+1)); ``` To me it looks like the first cast is affecting the `+1` so it will move 4 bytes (a pointer), and the second cast is for the result being an `unsigned long long *`. And the star in the outer shell on the inner cast is an "access this memory" star. - Is that right? - Why bother if the original definition was a pointer, and we're casting to another pointer, the `+1` will still be a 4 byte jump. - What would happen if I write instead `oldidt = *(oldidt+1);`? (Assuming the compiler doesn't complain, and gives out and exec) The declaration of `oldidt` was: ``` static unsigned long long *oldidt; ``` I'm calling the casts "first" and "second" according to invocation (left is the second).

Original source