Listing 8 5 IntArray-2

From ArduinoInfo
Jump to navigation Jump to search
/* Listing 8-5. Using an int array.
 
 Purpose: Display an int array using array indexes
 
 Dr. Purdum, Nov. 22, 2014
 */

void setup() {
  Serial.begin(9600);

  int greet[6];  // Notice this is an int now
  int *ptr;              // ...as is this
  int i;

  greet[0] = 0;     // Numbers now...
  greet[1] = 1;
  greet[2] = 2;
  greet[3] = 3;
  greet[4] = 4;
  greet[5] = 5;

  ptr = greet;

  for (i = 0; i < 5; i++) {
//    Serial.print(greet[i]);   // Change this line...
//--new version  
  Serial.print(*(greet + i));   // New line for Listing 8-5
  }
  Serial.println();

// The following line outputs only '0'
  ptr = greet;
  Serial.print(*ptr++);

}
void loop() {
}
/*
The output as compiled as above is:
---------------------( COPY )---------------------
01234
0
-----------------( END COPY )----------------------
*/