User Tools

Site Tools


notes:c4eng:fall2022:projects:sss0

This is an old revision of the document!


BACKGROUND

ARRAYS

Arrays are a type of variable that contain a number of elements within it. An array can hold any number of elements, be they a variable or a value, provided that these elements are all of the same data type and the number of elements is a predetermined finite value.

The first element in an array has an index of 0, the second element has an index of 1, and so on. Declaring an array is done similarly, yet slightly differently, than that of other variables. With other variables, they might be declared like this:

int index = 0;

For this, “int” is the data type of the variable, “index” is its name, and “0” is its value, this we know.

For arrays, however, rather than declaring some sort of array data type, we instead declare the data type of the elements that will populate our array, appending square brackets to the end of the variable name to indicate an array, with a number within those brackets to signify the number of elements in, or size of, that array (no number means 0). Using vcc0 as an example:

int pin[NUM_PINS];

The name of this array is called “pin” and holds “NUM_PINS” number of elements, and the data type of those elements is “int”.

Both calling and assigning the value of an element within an array after the array has been declared is once again done similarly, yet slightly differently, than that of other variables. The only difference is that, for an array, the index of the element being assigned must be within square brackets appended to the end of the name of the array. Once again using vcc0 as an example, for assigning:

pin[2] = 3;

This will assign the third value (index = 2) of the array “pin” to have a value of “3”, provided that the array has a size of at least three elements.

ELECTRONICS

PARTS

CIRCUIT

notes/c4eng/fall2022/projects/sss0.1664254883.txt.gz · Last modified: 2022/09/27 05:01 by rgerbino