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