Pointers and sharing memory

Hi, this is an odd request but with some very useful applications. Is it possible to use pointers to assign more than one variable to the same location in memory? For example, to define two consecutive bytes in an array to be also accessible as a single two-byte int? Then, changing the contents of the bytes would change the int, and vice versa.

I'm imagining code that looks something like this:

byte Array[10];                       // Define an array of 10 bytes
int *Integer = *Array[3];             // [Won't work!] Assign Integer as sharing location 3 (and 4) of the array
int Result;

Array[3] = 1;
Array[4] = 2;

Result = Integer;                     // Result would now be 258; ie 1*256 + 2

Integer = 513;

Result = Array[3];                    // Result would now be 2; the upper byte of 513
Result = Array[4];                    // Result would now be 1; the lower byte of 513

Yes, and its a trick often done, but exposes you to the underlying memory-addressing model (principally endianness). So be careful, and remember that some architectures are big-endian and some are little-endian. If none of this makes sense then Wikipedia will hopefully explain.

Is it possible to use pointers to assign more than one variable to the same location in memory? For example, to define two consecutive bytes in an array to be also accessible as a single two-byte int? Then, changing the contents of the bytes would change the int, and vice versa.

As MarkT says, the answer is yes, but a union is the more usual way of dealing with ints as bytes, longs as bytes, floats as bytes, etc.

Aha. Union. I'd wasn't familiar with that, but looking at it it seems just what I need. Thanks!