Can I make two variables share the same memory address?

In another language I work with, I can make two variables of different data-types reference the same patch of memory. I'd do it like this:

StringVar     cstring(10)   -- a string variable 
LongVar     long,over(StringVar)   -- a 4-byte numeric variable

The "over" syntax causes the second variable to use the same first 4 bytes as the first. With this I can access the data stored there in two different ways:

So if I do this:

Stringvar='AA' & chr(0) & chr(0)

LongVar will = 16705 (65*256 + 65)

or I can do this:

LongVar=66

and StringVar will = 'B'

Can I do this in Arduino C? Would I use "references" to accomplish this? I've been reading some posts about references but am getting confused.

Thanks! GB

A union is the conventional way to do it in c. You can, as you surmised, achieve your goal with pointers too.

Technically a union allows one variable to have several runtime types, it doesn't alias two variables - although a union will sort of do what's wanted here if you think of the selector as part of the variable name.

You can simply pass pointers/references around to achieve the effect too, but then the variables have to be explicitly dereferenced with '*' operator (and the references cast to the appropriate type). The union approach is neater I think.

Hey, that Union looks like just the ticket. Any reason it's not in the Arduino language reference? I also notice that when I type it in it doesn't take on the color of a reserved word, yet it seems to compile just fine.

It looks like just what I needed, but I'll post back once I've coded it in and tested it.

Thanks guys GB

Just wanted to post an update. "Union" worked like a charm -- thanks again to you guys.

If anyone's curious, I'm using it as part of my serial communications coding. I wanted to be able to pass/return any kind of data type (byte, long, string) using common code and data memory. Here's my Union definition:

union byteshare {             // received command data will be placed in CommandDataStr and then access by the appropriate data types
  char CommandDataStr[101];
  byte CommandDataByteA[101];
  byte CommandDataByte;
  int  CommandDataInt;
  long CommandDataLong;
} bs;

bs is right. The key to proper utilization of a union is that everything in the union is the same size.

So:

union byteshare {             // received command data will be placed in CommandDataStr and then access by the appropriate data types
  char CommandDataStr[4];
  byte CommandDataByteA[4];
  int  CommandDataInt[2];
  long CommandDataLong;
} bs;

Then, write the code to deal with some number of bs elements. That may involve, for instance, 2 bs's to receive 8 bytes, 8 chars, 2 ints, or 2 longs (or floats). Dynamically or statically allocating the correct number of these elements is important.

using common code and data memory.

Common memory, yes. Common code, no. You need different code to pack a long into this structure than to pack chars or bytes (and to unpack them, of course).

PaulS: bs is right. The key to proper utilization of a union is that everything in the union is the same size.

Common memory, yes. Common code, no. You need different code to pack a long into this structure than to pack chars or bytes (and to unpack them, of course).

Actually, I read up on Union in a C programming guide somewhere and it stated that you can have different sized variables in a Union (which I have going on here) and it will reserve enough bytes for the largest variable in the Union.

Common code, yes you're right that it won't all be common -- there will be some specific code to handle the different data types. The Union will also relieve me from the task of contructing and deconstructing ints and longs to/from individual bytes.