CIS 250 - Arrays
Objectives
- use pointers to access and manipulate array elements
- use multidimensional arrays
Arrays and pointer arithmetic
- declaring an array for ints: int num[10];
- declaring and initializing an array: int num[] = { 1, 2, 3, 4 };
- if an array is only partially initialized at declaration, the remaining
(uninitialized) elements are set to 0
- the name of an array is a pointer to the first element of the array
(num is equivalent to &num[0])
- you can use pointers to refer to arrays just as you can use subscripts
(*(num+2) is equivalent to num[2])
- see array1.cpp
- arrays can be passed to functions using just the array name since that points
to the beginning of the array in memory
- changes made to an array in a function change the actual array that was passed
since arrays are passed by reference only
- you can make sure the contents of a passed array aren't modified by specifying it
as a const in the formal argument list for the function
- if you want to make sure a function doesn't change the contents of an array,
place the keyword const in front of the array parameter in the
function definition and prototype
- since functions do not know how long an array is, you will usually have to
also pass the length of the array
- pointer arithmetic works differently than normal arithmetic since the number
being added must be multiplied by the size of the data type
- For example: if p is an int pointer which points to array n, then p points to element
n[0], and p+1 points to element n[1]. If this program is on a machine that has 4 byte
ints, the actual value of p+1 is 4 more than p
- see array2.cpp
Multidimensional arrays
- We won't go beyond two-dimensional arrays, which you can think of as a checkerboard
or spreadsheet grid (they have rows and columns).
- declaring a multidimensional array: int num[2][5];
- declaring and initializing amultidimensional array: int num[][] = { {1, 2}, {3, 4} };
- When you pass a multidimensional array, you have to specify every dimension except the first.
- see array3.cpp
- You can play all sorts of games with arrays since the elements of an array occupy
contiguous memory locations. See array4.cpp for an example
of how a multidimensional array can be manipulated by a function as if it is a
one-dimensional aray.
- C-style strings are character arrays with a terminating null ('\0') character
- It is often easier to use the new string class available in C++.
- See array5.cpp for examples of handling string arrays.
- See array6.cpp for examples of differences in handling the
different types of strings, including sorting and assignment.
Parallel arrays
- parallel arrays refer to two or more arrays where elements at a specific index are
all related
- parallel arrays are usually considered a poor practice
- parallel arrays can be useful at times, and you also have to know how to maintain code that already uses them
- parallel arrays can usually be easily replaced by a single array of structs where
the struct combines the data that was divided between the arrays