Array Basics


or,

Arrays: Part One


TOPICS:

  1. Ok. Just what is an array ?
  2. What's a index ?
  3. How do I declare an array ?
  4. Is it a type ? I thought it was declared with a type ?
  5. How big is my array ?
  6. One Dimensional Arrays ?
  7. What's different about Two Dimensional Arrays ?
  8. Can I do 3-d or 4-d arrays ?
  9. What are 'parallel' arrays ?
  10. Array Vocabulary : What does it mean when you say. . .?


Ok. Just what is an array ?

An array is a group of memory locations related by the fact that they all have the same name and same type. We refer to individual elements of the array by specifying the name of the array and the position of the particular element of the array.

Back to the Topic Index


What is an Index ?

An array index is just like an variable index in math. You can talk about a set of values, x, each with a subscript value, i, which goes from 1 to n.

In C arrays work on a similar principle. You have an array, A. It's elements are the subscript positions they hold in the array.

The First Element of any array in C is the ZEROTH element.

Element Number Array Position Element Name
1st 0 a[0] ("a sub 0")
2nd 1 a[1] ("a sub 1")
3rd 2 a[2] ("a sub 2")
nth n a[n-1] ("a sub n-1")
n+1st n a[n] ("a sub n")

Notice that the ith element of the array is in position i-1.

Back to the Topic Index


How do I declare an array ?

To declare an array, you must indicate what type of elements and and the number of those elements that it holds.

int A[9];
Defines an array of integers named "A" with 9 elements numbered 0 - 8.

float purple_people_eater[200];
Defines an array of floats named "purple_people_eater" with 200 elements numbered 0 - 199.

int A[] = {0,1,2,3,4,5,6,7,8};
Defines an integer array "A" with 9 elements (the number of values that follow inside the braces) with those values inside the braces as the initial values for the elements numbered 0 - 8.

char Joe[5];
Defines an array of characters named "Joe" with 5 elements numbered 0 - 4

char a[] = {'z', 'i', 'p', 'p', 'y'};
Defines an array of characters named "a" with 5 elements (the number of characters inside the braces) numbered 0 - 4 with initial values as follows:

Some character array definitions are special:
char a[] = "zippy";
Defines an array of characters named "a" with 6, not 5, elements
Number of elements =

The elements of this character array are numbered 0 - 5 and have initial values as follows:

An array can have an initializer like any other C variable.

All these declarations are examples of one-dimensional arrays. Two dimensional arrays are declared slightly differently.

Back to the Topic Index


Is it a type ? I thought it was declared with a type ?

An array is a data structure but it also has a type. An array is never just an array. You have an "array of integers", or an "array of floats", or an "array of structs", etc. Each one of these is a different type of array.

Arrays are declared with a type. The type specifies what type of elements make up the array. The type is the same for all elements in the array.

Back to the Topic Index


How big is my array ?

Arrays are statically declared. They never change size or type during the execution of your program.

If you declare an array:

You've declared an array named "zippy" that has 29 elements numbered 0 - 28. The size of your array is "29".

You can find out the size of any array by using the sizeof() function. However, the result you get from the sizeof() function is misleading. It will tell you exactly how much memory is allocated to your array, NOT the number of elements the array has.

Back to the Topic Index


One Dimensional Arrays

This is the most basic array in C.

int joe[] = {10, -1, 312, 99, -54, 12};
Defines an integer array "joe" with 6 elements with initial values as defined in the initializer list.

The elements and their values are as shown below:

Array Element Value
joe[0] 10
joe[1] -1
joe[2] 312
joe[3] 99
joe[4] -54
joe[5] 12

Back to the Topic Index


What's different about Two Dimensional Arrays ?

Two dimensional arrays are defined with two size specifications.

int joe[2][3] = {10, -1, 312, 99, -54, 12};

Joe has 2 rows and three columns. You can think of "joe" as looking like this:

Rows\ColsCol 1Col 2Col 3
Row 1 (0,0) (0,1) (0,2)
Row 2 (1,0) (1,1) (1,2)

The Cell coordinates correspond to the array index values. Remember that Cell "N" is refered to by indexing "N-1"

The elements and their values for this same array:

are shown in the table below:

Row NumberColumn Number Array ElementValue
1 1 joe[0][0] 10
1 2 joe[0][1] -1
1 3 joe[0][2] 312
2 1 joe[1][0] 99
2 2 joe[1][1] -54
2 3 joe[1][2] 12

Back to the Topic Index


Can I make 3-d or 4-d arrays ?

Yes, but it might be better to simulate larger dimensions with parallel arrays. Remember, each dimension adds a complexity to what your doing and you can eat space up fast this way. Before you go making large-dimension arrays you should be careful to plan out what you'll be doing.

Back to the Topic Index


What are 'parallel' arrays ?

Parallel arrays are a technique of using several arrays of the same size. You can use the "same cell" in different arrays to refer to the "same" larger item.

For example, say we have three arrays:

We can consider the 3rd cell in each array as refering to the same individual.

Give Sally the first cell in each array. Then, if you want to get Sally's employee number, you refer to employee[0]. If you want Sally's grade, you can refer to grade[0]. And salary[0] will refer to Sally's salary.

The first cell in each array holds the relevant information about Sally.

As long as you maintain the integrity of the cell numbers as you step across arrays, you can use as many parallel arrays as you want to hold information about the 'same' individual.

Back to the Topic Index


Back to the Top of this Page


Back to the Top of this page


[Back to the Index]

graphics images Copyright 1998-2003 Elizabeth Fraley HTML code copyright 1998-2003 Elizabeth Fraley. Permission is given to provide these pages in their original, unaltered form online for educational purposes. They may not be republished or included on a CDROM, disk or other media or with any published work without the express permission of the copyright holder (this includes FAQ books).