Framework (A-Z)

Reference for Wiring version 1.0 Build 0100+ If you have a previous version, use the reference included with your software. If see any errors or have any comments, let us know.

Name

& (reference operator)

Examples
int x = 0;
int *pointer_to_x = &x;  

void setup() {
  Serial.begin(9600);
}

void loop() {
 // increments by one the value of the data pointer_to_x points to.
 (*pointer_to_x) += 1;
 // x is now equal to x + 1
 Serial.print("x value is: ");
 Serial.println(x);
 Serial.print("x address is: ");
 Serial.println((int)pointer_to_x);
 delay(100);
}

// pointers are useful for returning multiples value on a function
int temperature = 0;
int humidity = 0;  

void setup() {
  Serial.begin(9600);
}

void loop() {
 // send a reference to the variables 
 // to update their values
 updateVariables(&temperature, &humidity);
 Serial.print("temperature value is: ");
 Serial.println(temperature);
 Serial.print("humidity value is: ");
 Serial.println(humidity);
 delay(100);
} 

// receive pointers to variables as parameters
// and update the values pointed
void updateVariables(int *temp, int *humid) {
  (*temp) += 1;
  (*humid) += 10;
}
Description FOR ADVANCED USERS: Althought pointers are not used/required in the Wiring language, they exist and can be used as the Wiring language is an extension of C/C++. The following documentation attempts to explain them in an easy manner. A pointer is a data type whose value refers directly to (or "points to") another value stored elsewhere in memory using its address. A pointer references a location in memory, and obtaining the value at the location a pointer refers to is known as dereferencing the pointer. Pointers to data significantly improve performance for repetitive operations such as traversing strings, lookup tables, control tables and tree structures. In particular, it is often much "cheaper" in time and space to copy and dereference pointers than it is to copy and access the data to which the pointers point. The reference operator or address-of operator, denoted by "&", is a unary operator. It operates on a variable, and returns the memory address of the variable. This is called "referencing" the variable. From the example provided the statement int *pointer_to_x = &x; references the variable x, get the address in memory of variable x assigns it to pointer_to_x.
Syntax
&variable
Parameters
variable any primitive or compound datatype variable
Usage Application
Related dereference
array
Updated on July 07, 2011 11:08:51pm PDT

Creative Commons License