& (reference operator)

Description

Referencing is one of the features specifically for use with pointers. The ampersand operator

&
is used for this purpose. If
x
is a variable, then
&x
represents the address of the variable
x
.

Example Code

1int *p; // declare a pointer to an int data type
2 int i = 5;
3 int result = 0;
4 p = &i; // now 'p' contains the address of 'i'
5 result = *p; // 'result' gets the value at the address pointed by 'p'
6 // i.e., it gets the value of 'i' which is 5

Notes and Warnings

Pointers are one of the complicated subjects for beginners in learning C, and it is possible to write the vast majority of Arduino sketches without ever encountering pointers. However for manipulating certain data structures, the use of pointers can simplify the code, and knowledge of manipulating pointers is handy to have in one’s toolkit.

See also

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.