Reference.Array History

Hide minor edits - Show changes to output

July 24, 2009, at 01:32 AM by Paul Badger -
Changed lines 59-60 from:
to:
*[[PROGMEM]]
June 25, 2009, at 01:15 PM by Paul Badger -
Changed lines 35-36 from:
Unlike in some versions of BASIC, the C compiler does no checking to see if array access is within legal bounds of the array size that you have declared.
to:
Unlike BASIC or JAVA, the C compiler does no checking to see if array access is within legal bounds of the array size that you have declared.
June 25, 2009, at 01:08 PM by Paul Badger -
Changed lines 52-53 from:
}
@]
to:
}@]
Added lines 57-59:
!!!!See also
* [[http://arduino.cc/en/Reference/VariableDeclaration|Variable Declaration]]
January 11, 2008, at 05:49 AM by Paul Badger -
Changed lines 35-36 from:
Unlike in some version of BASIC, the C compiler does no checking to see if array access is within legal bounds of the array size that you have declared.
to:
Unlike in some versions of BASIC, the C compiler does no checking to see if array access is within legal bounds of the array size that you have declared.
May 27, 2007, at 03:07 AM by Paul Badger -
Added lines 35-36:
Unlike in some version of BASIC, the C compiler does no checking to see if array access is within legal bounds of the array size that you have declared.
May 27, 2007, at 03:05 AM by Paul Badger -
Added lines 32-34:

For this reason you should be careful in accessing arrays. Accessing past the end of an array (using an index number greater than your declared array size - 1) is reading from memory that is in use for other purposes. Reading from these locations is probably not going to do much except yield invalid data. Writing to random memory locations is definitely a bad idea and can often lead to unhappy results such as crashes or program malfunction. This can also be a difficult bug to track down.
Deleted lines 55-58:
!!!! Coding Tip

Be careful in accessing arrays. Accessing past the end of an array (using an index number greater than your declared array size - 1) is reading from memory that is in use for other purposes. Reading from these locations is probably not going to do much except yield invalid data. Writing to random memory locations is definitely a bad idea and can often lead to unhappy results such as crashes or program malfunction. This can also be a difficult bug to track down.
May 27, 2007, at 03:01 AM by Paul Badger -
Changed line 26 from:
It also means that in an array with ten elements, that index nine is the last element. Hence:
to:
It also means that in an array with ten elements, index nine is the last element. Hence:
May 27, 2007, at 03:01 AM by Paul Badger -
Changed line 30 from:
// myArray[10] is invalid and contains random information (other memory address)
to:
// myArray[10] is invalid and contains random information (other memory address)
May 27, 2007, at 03:00 AM by Paul Badger -
Changed lines 31-32 from:

to:
@]
May 27, 2007, at 03:00 AM by Paul Badger -
Changed line 26 from:
!!!!To assign a value to an array:
to:
It also means that in an array with ten elements, that index nine is the last element. Hence:
Added lines 28-34:
int myArray[10]={9,3,2,4,3,2,7,8,9,11};
// myArray[9] contains 11
// myArray[10] is invalid and contains random information (other memory address)


!!!!To assign a value to an array:
[@
Changed lines 56-57 from:
Be careful in accessing arrays. Accessing past the end of an array is reading from memory that is in use for other purposes. Reading from these locations is probably not going to do much except yield invalid data. Writing to random memory locations is definitely a bad idea and can often lead to unhappy results such as crashes or program malfunction. This can also be a difficult bug to track down.
to:
Be careful in accessing arrays. Accessing past the end of an array (using an index number greater than your declared array size - 1) is reading from memory that is in use for other purposes. Reading from these locations is probably not going to do much except yield invalid data. Writing to random memory locations is definitely a bad idea and can often lead to unhappy results such as crashes or program malfunction. This can also be a difficult bug to track down.
May 27, 2007, at 02:52 AM by Paul Badger -
Deleted line 42:
May 27, 2007, at 02:51 AM by Paul Badger -
Changed line 23 from:
Arrays are '''zero indexed''', that is, refering to the assignment above, the first element of the array is at index 0, hence\\\
to:
Arrays are '''zero indexed''', that is, referring to the array initialization above, the first element of the array is at index 0, hence\\\
May 27, 2007, at 02:49 AM by Paul Badger -
Changed line 23 from:
Arrays are '''zero indexed''', that is, the first element of the array is at index 0, hence\\\
to:
Arrays are '''zero indexed''', that is, refering to the assignment above, the first element of the array is at index 0, hence\\\
April 18, 2007, at 09:00 AM by David A. Mellis -
Changed line 11 from:
int myPins[] = {1, 5, 17, -2, 3};
to:
int myPins[] = {2, 4, 8, 3, 6};
Added line 13:
char message[6] = "hello";
Changed lines 17-18 from:
In myPins we declare an array without explicitly choosing a size. The compiler counts the elements and adds one more element for a required null character terminator.
to:
In myPins we declare an array without explicitly choosing a size. The compiler counts the elements and creates an array of the appropriate size.
Changed line 40 from:
Serial.println(pins[i]);
to:
Serial.println(myPins[i]);
April 18, 2007, at 03:29 AM by Paul Badger -
Changed line 25 from:
'''To assign a value to an array:'''
to:
!!!!To assign a value to an array:
Changed lines 27-28 from:
mySensVals[0] = 10;
@] '''To retrieve a value from an array:'''
to:
mySensVals[0] = 10;@]

!!!!To retrieve a value from an array:
Changed lines 34-35 from:
Arrays are often manipulated inside '''for''' loops, where the loop counter is used as the index for each array element. To print the elements of an array over the serial port, you could do something like this:
to:
Arrays are often manipulated inside '''for''' loops, where the loop counter is used as the index for each array element. For example, to print the elements of an array over the serial port, you could do something like this:
April 18, 2007, at 03:27 AM by Paul Badger -
Changed lines 28-29 from:
@]
'''To retrieve a value from an array:'''
to:
@] '''To retrieve a value from an array:'''
Added lines 31-32:
!!!!Arrays and FOR Loops
April 18, 2007, at 03:26 AM by Paul Badger -
Changed lines 18-19 from:
Finally you can both initialize and size your array, as in mySensVals. Note however that one more element than your initialization is required, to hold the required null character.
to:
Finally you can both initialize and size your array, as in mySensVals. Note that when declaring an array of type char, one more element than your initialization is required, to hold the required null character.
Changed lines 22-24 from:
Arrays are '''zero indexed''', that is, the first element of the array is at index 0, hence @@mySensVals[0] == 2, mySensVals[1] == 4,@@ and so forth.

To assign a value to an array, do something like this
to:
Arrays are '''zero indexed''', that is, the first element of the array is at index 0, hence\\\
@@mySensVals[0] == 2, mySensVals[1] == 4,@@ and so forth.

'''To assign a value to an array:'''
Changed line 29 from:
To retrieve a value from an array, something like
to:
'''To retrieve a value from an array:'''
April 17, 2007, at 06:49 AM by Paul Badger -
Changed lines 22-23 from:
Arrays are '''zero indexed''', that is, the first element of the array is at index 0, hence @@mySensVals[0] == 2, mySensVals[1] == 4, and so forth.
to:
Arrays are '''zero indexed''', that is, the first element of the array is at index 0, hence @@mySensVals[0] == 2, mySensVals[1] == 4,@@ and so forth.
April 17, 2007, at 06:47 AM by Paul Badger -
Changed lines 22-23 from:
'''Arrays are zero indexed''', that is, the first element of the array is at index 0 so @@mySensVals[0] == 2, mySensVals[1] == 4, and so forth.
to:
Arrays are '''zero indexed''', that is, the first element of the array is at index 0, hence @@mySensVals[0] == 2, mySensVals[1] == 4, and so forth.
April 17, 2007, at 06:46 AM by Paul Badger -
Changed lines 16-19 from:
In myPins we declare an array without explicitly choosing a size. The compiler counts the elements and adds one more element for a required null character.

Finally you can both initialize and size your array. Note however that one more element than your initialization is required, to hold the required null character.
to:
In myPins we declare an array without explicitly choosing a size. The compiler counts the elements and adds one more element for a required null character terminator.

Finally you can both initialize and size your array, as in mySensVals. Note however that one more element than your initialization is required, to hold the required null character.
Changed lines 31-32 from:
To print the elements of an array over the serial port, you could do something like this:
to:
Arrays are often manipulated inside '''for''' loops, where the loop counter is used as the index for each array element. To print the elements of an array over the serial port, you could do something like this:
April 17, 2007, at 06:42 AM by Paul Badger -
Changed lines 3-4 from:
An array is a collection of variables that are accessed with an index number. Arrays in the C programming language, on which Arduino is based, can be complicated, but using simple arrays is relatively easy.
to:
An array is a collection of variables that are accessed with an index number. Arrays in the C programming language, on which Arduino is based, can be complicated, but using simple arrays is relatively straightforward.
April 17, 2007, at 06:41 AM by Paul Badger -
Changed lines 7-8 from:
To create an array, of, say, 5 integers, place a statement like this at the top of your sketch or the start of a function:
to:
All of the methods below are valid ways to create (declare) an array.
Changed lines 10-12 from:
int pins[5];
to:
int myInts[6];
int myPins[] = {1, 5, 17, -2, 3};
int mySensVals[6] = {2, 4, -8, 3, 2};
Changed lines 15-16 from:
If you want to give the elements of the array initial values, use a statement like this instead:
to:
You can declare an array without initializing it as in myInts.\\\
In myPins we declare an array without explicitly choosing a size. The compiler counts the elements and adds one more element for a required null character.

Finally you can both initialize and size your array. Note however that one more element than your initialization is required, to hold the required null character.

!!!!Accessing an Array

'''Arrays are zero indexed''', that is, the first element of the array is at index 0 so @@mySensVals[0] == 2, mySensVals[1] == 4, and so forth.

To assign a value to an array, do something like this
Changed line 26 from:
int pins[5] = { 11, 3, 5, 4, 2 };
to:
mySensVals[0] = 10;
Changed lines 28-34 from:

This will assign the value 11 to the first elements in the array, the value 3 to the second, etc.

!!!!Accessing an Array

The first thing you need to know is that '''arrays are zero indexed''', that is, the first element of the array is at index 0 and is accessed with a statement like:
to:
To retrieve a value from an array, something like
[@x = mySensVals[4]@];

To print the elements of an array over the serial port, you could do something like this:
Changed lines 34-37 from:
pins[0] = 10;
to:
int i;
for (i = 0; i < 5; i = i + 1) {
Serial.println(pins[i]);
}
Deleted lines 39-47:
To print the elements of an array over the serial port, you could do something like this:

[@
int i;
for (i = 0; i < 5; i = i + 1) {
Serial.println(pins[i]);
}
@]
April 17, 2007, at 06:19 AM by Paul Badger -
Changed lines 3-4 from:
An array is a group of variables that is accessed with an index number. Arrays in the C programming language, on which Arduino is based, can be complicated, but simple things should be relatively easy. The basics operations are:
to:
An array is a collection of variables that are accessed with an index number. Arrays in the C programming language, on which Arduino is based, can be complicated, but using simple arrays is relatively easy.
April 16, 2007, at 06:50 PM by Paul Badger -
Changed lines 5-6 from:
!!!!Creating (declaring) an array
to:
!!!!Creating (Declaring) an Array
Changed lines 21-22 from:
!!!Accessing an Array
to:
!!!!Accessing an Array
Changed lines 38-39 from:
!!!Example
to:
!!!!Example
April 16, 2007, at 06:48 PM by Paul Badger -
Changed lines 5-6 from:
!!!!Creating an Array
to:
!!!!Creating (declaring) an array
April 16, 2007, at 06:44 PM by Paul Badger -
Changed lines 44-45 from:
Be careful in accessing arrays. Accessing past the end of an array is reading from memory that is in use for other purposes. Reading from these locations is probably not going to do much except yield invalid data. Writing to random memory locations is definitely a bad idea and can often lead to unhappy results such as crashes or program malfunction. This can also be a difficult bug to track down.
to:
Be careful in accessing arrays. Accessing past the end of an array is reading from memory that is in use for other purposes. Reading from these locations is probably not going to do much except yield invalid data. Writing to random memory locations is definitely a bad idea and can often lead to unhappy results such as crashes or program malfunction. This can also be a difficult bug to track down.
April 16, 2007, at 06:43 PM by Paul Badger -
Changed lines 3-6 from:
Arrays in the C programming language, on which Arduino is based, can be complicated, but simple things should be relatively easy. The basics operations are:

!!!Creating an Array
to:
An array is a group of variables that is accessed with an index number. Arrays in the C programming language, on which Arduino is based, can be complicated, but simple things should be relatively easy. The basics operations are:

!!!!Creating an Array
April 16, 2007, at 06:40 PM by Paul Badger -
Changed lines 1-2 from:
!Arrays
to:
!!Arrays
Changed lines 42-43 from:
!!!Tip
to:
!!!! Coding Tip
April 16, 2007, at 05:13 AM by Paul Badger -
April 16, 2007, at 05:12 AM by Paul Badger -
Changed lines 42-43 from:
!!!!Tip
to:
!!!Tip
April 16, 2007, at 05:11 AM by Paul Badger -
Deleted lines 37-40:
Tip

Be careful in accessing arrays. Accessing past the end of an array is reading from memory that is in use for other purposes. Reading from these locations is probably not going to do much except yield invalid data. Writing to random memory locations is definitely a bad idea and can often lead to unhappy results such as crashes or program malfunction. This can also be a difficult bug to track down.
Changed lines 40-44 from:
For a complete program that demonstrates the use of arrays, see the [[http://www.arduino.cc/en/Tutorial/KnightRider | Knight Rider example]] from the [[http://www.arduino.cc/en/Main/LearnArduino | Tutorials]].
to:
For a complete program that demonstrates the use of arrays, see the [[http://www.arduino.cc/en/Tutorial/KnightRider | Knight Rider example]] from the [[http://www.arduino.cc/en/Main/LearnArduino | Tutorials]].

!!!!Tip

Be careful in accessing arrays. Accessing past the end of an array is reading from memory that is in use for other purposes. Reading from these locations is probably not going to do much except yield invalid data. Writing to random memory locations is definitely a bad idea and can often lead to unhappy results such as crashes or program malfunction. This can also be a difficult bug to track down.
April 16, 2007, at 05:10 AM by Paul Badger -
Added lines 38-41:
Tip

Be careful in accessing arrays. Accessing past the end of an array is reading from memory that is in use for other purposes. Reading from these locations is probably not going to do much except yield invalid data. Writing to random memory locations is definitely a bad idea and can often lead to unhappy results such as crashes or program malfunction. This can also be a difficult bug to track down.
August 12, 2006, at 07:00 PM by David A. Mellis - adding link to Knight Rider example
Changed lines 36-40 from:
@]
to:
@]

!!!Example

For a complete program that demonstrates the use of arrays, see the [[http://www.arduino.cc/en/Tutorial/KnightRider | Knight Rider example]] from the [[http://www.arduino.cc/en/Main/LearnArduino | Tutorials]].
August 01, 2006, at 09:01 PM by David A. Mellis -
Changed lines 5-6 from:
!!Creating an Array
to:
!!!Creating an Array
Changed lines 21-22 from:
!!Accessing an Array
to:
!!!Accessing an Array
August 01, 2006, at 08:57 PM by David A. Mellis -
Changed lines 23-36 from:
The first thing you need to know is that '''arrays are zero indexed''', that is, the first element of the array is at index 0.
to:
The first thing you need to know is that '''arrays are zero indexed''', that is, the first element of the array is at index 0 and is accessed with a statement like:

[@
pins[0] = 10;
@]

To print the elements of an array over the serial port, you could do something like this:

[@
int i;
for (i = 0; i < 5; i = i + 1) {
Serial.println(pins[i]);
}
@]
August 01, 2006, at 08:39 PM by David A. Mellis -
Added lines 1-23:
!Arrays

Arrays in the C programming language, on which Arduino is based, can be complicated, but simple things should be relatively easy. The basics operations are:

!!Creating an Array

To create an array, of, say, 5 integers, place a statement like this at the top of your sketch or the start of a function:

[@
int pins[5];
@]

If you want to give the elements of the array initial values, use a statement like this instead:

[@
int pins[5] = { 11, 3, 5, 4, 2 };
@]

This will assign the value 11 to the first elements in the array, the value 3 to the second, etc.

!!Accessing an Array

The first thing you need to know is that '''arrays are zero indexed''', that is, the first element of the array is at index 0.

Share