Is there anaybody who has a short and clear program that demonstrates the working of pointers.
http://arduino.cc/en/Reference/Pointer doesn't help me.
I read about * and &, but don't know when to use.
Problem: I need to change the value of a global parameter within several funtions (I know that global parameters can be read everywhere, but I need to change parameters).
a pointer is a variable that holds an address of another variable (often an array or stuct)
int x = 5;
int y = 7;
int ar[] = { 3,4,3,2,1,0 };
void setup()
{
Serial.begin(115200);
int *p = &x; // p is a pointer that holds the address of x
Serial.println( *p, DEC); // print content of the address p points to
p = &y; // p now holds the address of y
Serial.println( *p, DEC); // print content of the address p points to
p = &ar[0]; // p now holds the address of the first element of the array (Note zero based)
Serial.println( *p, DEC); // print content of the address p points to
for (p = &ar[0]; *p != 0; p++)
{
Serial.println( *p, DEC); // print content of the address p points to
}
}
void loop()
{
}
similar for other datatypes
Dank je wel Rob. (Thanx Rob)
Welcome
p = &ar[0];
is often written as
p = ar;
as the name of an array equals the address of its first element.
However the p = &ar[0]; is more informative imho
Note you can also have pointers to functions (with the same signature)
pointers are often used to go fast through an array, check code of strcpy, strlen