As I work on my Arduinos I am constantly reminded that I always found pointer based languages hard to master. I guess that is why I didnt make a career out of working in C or C++. But the Arduino demands it if you are going to do anything complicated. I have been struggling to remember my CS classes of so long ago but have gotten a lot of help from folks on the
Arduino Forum.
With that help in hand I came up with a sketch (
available here) that runs through most of the pointer combinations and permutations that I think I will need for the immediate future. The output from this sketch is shown below:
Pointer to an integer
a is now = 200
Another way to use that pointer...
a is now = 300
...which allows you to do this
a is now = 301
Pointer to a char array
abcdefghijklmnopqrstuvwxyz0123456789 <- Printed using original assignment of 'myS'
a <- Printed using '*myS'
a <- Printed using pointer '*ptrmyS'
abcdefghijklmnopqrstuvwxyz0123456789 <- Printed using pointer 'ptrmyS'
abcdefghijklmnopqrstuvwxyz0123456789 <- Printed by walking pointer down char array
abcde#ghijklmnopqrstuvwxyz0123456789 <- after modifying using array notation
abcde#ghij#lmnopqrstuvwxyz0123456789 <- after modifying using pointer notation
Printed in a function using a variable...
abcde#ghij#lmnopqrstuvwxyz0123456789 ...passed using original assignment
abcde#ghij#lmnopqrstuvwxyz0123456789 ...passed using pointer '*ptr2myS
abcde#ghij#lmno#qrstuvwxyz0123456789 ...passed using pointer '*ptr2myS and modified
Pointers to an integer array...
...int *B = A + 5; = 5
...int C = *B + 10; = 15
...*B = 21; = 21
Pointers into a two dimensional array...
abcdefg <- Printed as myData[3]
d <- Printed as myData[3][3]
e <- Printed as *( myData[3] + 4 )
abcdefg <- Printed as myDataAsPtr[3]
defg <- Printed as myDataAsPtr[3] + 3
a <- Printed as *myDataAsPtr[3]
t <- Printed as *( myDataAsPtr[1] + 3 )
22222 <- Printed as *ptr after 'const char (*ptr)[10] = (myData) + 2;'
Pointers into a two dimensional array...
abcdefg <- Printed as myData[3]
d <- Printed as myData[3][3]
e <- Printed as *( myData[3] + 4 )
abcdefg <- Printed as myDataAsPtr[3]
defg <- Printed as myDataAsPtr[3] + 3
a <-
Printed as *myDataAsPtr[3]
t <- Printed as *( myDataAsPtr[1] + 3 )
wertyu <- Printed as *ptr1 after 'const char (*ptr1)[10] = (myData) + 2;'
Pointers into an array of structures...
line2 <- Printed via function passed a pointer to a row
selGraph <- Printed via a pointer to a row
selStats
selGraph
line2
<- Printed via function passed a pointer to a row
Thanks!
ReplyDelete