]> git.piffa.net Git - sketchbook_andrea/blob - basic_programming/pointers_c/pointers_3_resetting_before_use/pointers_3_resetting_before_use.ino
02642eb5af15069b54962d3496714d4ea778a93b
[sketchbook_andrea] / basic_programming / pointers_c / pointers_3_resetting_before_use / pointers_3_resetting_before_use.ino
1 /*
2 Purpose: Illustrate pointer arithmetic
3  Dr. Purdum, August 20, 2012
4  */
5 #include <string.h>
6 void setup() {
7   Serial.begin(9600);
8 }
9 void loop() {
10   char buffer[50];
11   char *ptr;
12   int i;
13   int length;
14
15   strcpy(buffer, "When in the course of human events");
16   ptr = buffer;
17   length = strlen(buffer);
18   Serial.print("The lvalue for ptr is: ");
19   Serial.print((unsigned int)&ptr);
20   Serial.print(" and the rvalue is ");
21   Serial.println((unsigned int)ptr);
22   while (*ptr) {
23     Serial.print(*ptr++); // This actually incrementa ptr* + 34
24   }
25   Serial.println("");
26
27   Serial.println("Lenght of the string is: ");
28   Serial.println(length);  
29   Serial.println("");
30
31
32   // ptr = ptr - length ; // Whis would roll back ptr
33   for (i = 0; i < length; i++) {
34     Serial.print(*(ptr + i));  
35     // Serial.print(*(ptr + i- lenght)); // ptr is one lenght up ahead
36   }
37   // ptr = buffer  ;    // Right thing to do: reset the pointer before use
38   //   for (i = 0; i < length; i++) {
39   // Serial.print(*(ptr + i))
40
41
42   Serial.flush();
43   // Make sure all the data is sent...
44   exit(0);
45 }
46
47
48
49