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

* (dereference 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 Wiring 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 dereference operator or indirection operator, denoted by "*", is a unary operator. It operates on a pointer variable, and returns an l-value (or location) equivalent to the value at the pointer address. This is called "dereferencing" the pointer. From the example provided the statement (*pointer_to_x) += 1; dereferences the pointer pointer_to_x, and increments by one the value of the data it points to. In variables it is important to distinguish between the R-value (or contents) and the L-value (or location).
Syntax
datatype *pointervariable
*pointervariable
Parameters
pointervariable any primitive or compound datatype pointer variable
datatype any primitive or compound datatype: int, double, char, etc.
Usage Application
Related reference
array
Updated on July 07, 2011 11:08:07pm PDT

Creative Commons License