Accessing bytes in a long long variable with pointers

Viewed 938

I'm supposed to create a variable

long long hex = 0x1a1b2a2b3a3b4a4bULL;

and then define 4 pointers that point to 1a1b, 2a2b, 3a3b and 4a4b. I'm then printing the addresses and values of those double bytes.

My approach was to create a pointer

long long *ptr1 = &hex;

and then use pointer arithmetic to get to the next value. What I realized was that incrementing this pointer would increment it by long long bytes and not by 2 bytes like I need it to. Creating a short pointer

short *ptr1 = &hex;

Is what I would need but my compiler won't let me since the data types are incompatible. How do I get around that? Is there a way to create a pointer that increments by 2 bytes and assign that to a variable of a larger data type?

4 Answers
Related