myggle:
That is the explaination given for both (*) dereference and (&) reference operators in the Reference section. Can someone add to this with maybe some examples or other links to descriptive information about the subject?
I'm working my way through the Webserver Tutorial and came across the single (&) ampersand and my little hamster fell off his wheel so to speak.
Take a look at this demo. Hopefully it will help.
// demo - free software
char buffer [64];
void setup (void)
{
while (!Serial);
Serial.begin (115200);
uint16_t x;
uint16_t *y; // note the "*"
x = 0x1234; // set x to 0x1234
y = &x; // pointer y == address of x in ram
sprintf (buffer, "value of \"x\" (x) = 0x%02x\n\n", x);
Serial.print (buffer);
sprintf (buffer, "address of \"x\" in ram (&x) = 0x%02x\n\n", &x);
Serial.print (buffer);
sprintf (buffer, "value of pointer to address of \"x\" (y) = 0x%02x\n\n", y);
Serial.print (buffer);
sprintf (buffer, "value of what pointer \"y\" points to (*y) = 0x%02x\n\n", *y);
Serial.print (buffer);
sprintf (buffer, "address of pointer \"y\" in ram (&y) = 0x%02x\n\n", &y);
Serial.print (buffer);
}
void loop (void)
{
// nothing
}
...and it prints:
[b]value of "x" (x) = 0x1234
address of "x" in ram (&x) = 0x21f2
value of pointer to address of "x" (y) = 0x21f2
value of what pointer "y" points to (*y) = 0x1234
address of pointer "y" in ram (&y) = 0x21f4[/b]
(edit to add): Notice that the address in ram of x and y are sequential and 2 bytes apart. This is because both variables are 16 bit, so they each take up 2 bytes. Note that the variables happen to be sequential, but they don't have to be and you should never count on it.
X resides in 0x21F2 and 0x21F3,
Y resides in 0x21F4 and 0x21F5.
The AVR processor is little-endian which means, for example, looking at memory you would see this:
0x21F2 - 34
0x21F3 - 12
0x21F4 - F2
0x21F5 - 21
On a "big-endian" processor such as Motorola, the "0x1234" would be stored like this:
0x21F2 - 12
0x21F3 - 34
See that 0x21F4 and 0x21F5 (the variable Y) contain the value "0x21F3" which is the address of X in memory. Because of this, I can print the value of Y (Y), the value of what Y points to (*Y) or the address in ram where Y is stored (&Y).
Hope all this helps.