[last updated: 2022-08-05]
go to: Arduino programming
-----
- A pointer is a variable.
However unlike other variables, where the variable will contain some value that is used elsewhere in your program,
the value that a pointer contains is a memory location (always 2 bytes long) where your value of interest is stored.
- Define a pointer:
defines a variable Aptr.
the '*' means it is a pointer
the "int" type declaration does not refer to Aptr itself (which is always a memory location - whatever type that is...),
but instead refers to the type of the data that is stored at the memory location that Aptr points to.
char *Aptr would define Aptr as a pointer to a memory location that contains data of type char.
- " * " can be read as:
"the content of...'"
or: "a pointer to a memory location, the contents of which are ..."
- " & " can be read as: "the address of ..."
ptr = &var1;
sets ptr to the memory "address of" the variable var1
- Combine these two into one statement:
int *Aptr = &var1;
defines Aptr as a pointer that refers to the memory location where the value of the variable var1 is stored.
- Examples:
- int *Cptr; // declare Cptr as a pointer to an int data type
int var2 = 5;
int var3 = 0; // nom
Cptr = &var2; // now Cptr contains "the address of" var2
var3 = *Cptr; // var3 gets "the contents of" the memory address contained in Cptr (which is the address of var2)
this virtually does: var3 = var2 (= 5)
- *Cptr = 20; "the content of" memory address Cptr is set to 20
puts 20 at the memory address held by Cptr (which from prev code is the memory address of var2)
Since "the content of Cptr" is var2
this is equivalent to: var2 = 20
- Other stuff...:
- int *ptr;
and: int* ptr;
do exactly the same thing.
- However, if declaring more than one on a line, then
int *a, b;
and: int* a, b;
while the two commands also do the same thing,
what they do is declare a pointer a and an int b (ie. NOT a pointer b)
(at least according to one forum note which I have not tested/verified...)
Need to do some test code to figure out more completely how this all works...
- Links to check out:
https://ucexperiment.wordpress.com/2016/03/28/arduino-inline-assembly-tu...
YouTube: https://sites.google.com/site/arduinowarrior/15-assembly-functions-array...
https://mypractic.com/lesson-15-pointers-in-c-for-arduino-conversion-of-...
.
.
.
eof