Computer Science E50b . Tuesday March 2, 1999 . 7pm . Science Center 111
ARRAYS
What is an array?
- it is a collection of objects of the same type
- it has a size
- there is one object of the collection per index, you use indices
to access a given object
- Indices start at 0 and end at size-of-array - 1
- objects of the collection are stored in contiguous memory
How to create arrays (declaration)
object-type name-of-array [size-of-array];
Ex:
int ps1-grades[36]; // creates an array called
ps1-grades with 36 elements of type int
char name[5]; // creates an array called name with 5 elements of
type char
How to access the value of an object stored in an array
Suppose you want to access the 1st element of the array name, and
store this character in a variable c:
char c = name[0];
How to assign values to different objects stored in the array
There are different things you can do at different times to achieve this:
- You may initialize the array at declaration time as follows:
object-type name-of-array [size-of-array]= {elt1-value,
elt2-value,..., eltsize-value};
Note: you don't need to specify the size of the array in this case,
since it will figure out the size automatically. However, it is a good
idea to still write it there.
Ex:
int grades[5]={97, 56, 90, 74, 80};
char name[] = "Bob"; // This creates an array of type char and of
size 4! Remember that there is a \0 at the end of a string
- You may also assign a value to a given object at index i after
initialization as follows:
variable-of-sane-type-as-array = name-of-array [i];
Ex: name[1] = 'a'; // name is now Bab
- You can't assign an array to another array!
- You can't assign anything to an array after declaration. A good
rule is that you can't have an array on the left side of an =
Multi-dimensional arrays
Multi-dimensional arrays are just like 1-d arrays, except that there
are more than one [].
Ex: declaration of a 2-d array:
object-type name-of-array [size1] [size2]; // you could think of
this as a table, size1 being the number of columns and size2 being the
number of lines, this becomes tricky to visualize with more than 3-d
Would this be useful for a tic-tac-toe game for example?
Passing arrays to functions (Deitel p.241)
- An array should always be passed by reference to a function (why?).
- However, passing by reference means that you can modify it. If the
array should NOT be modified in the function, you should add the
reserved word const in front of it.
- Finally, you want to pass the size of the array to the function (why?).
Ex:
void modifyArray (int a [], int size) { definition of modify array}
void cantModifyArray (const int a[], size) {}
int main() {
const int MYSIZE = 5;
int myarray[MYSIZE];
modifyArray (myarray, MYSIZE);
cantModifyArray (myarray, MYSIZE);
}
Some things to watch for
- Very important: Because array indices start at zero, the last index
of the array is one less than the size of the array!
- If you get an error at execution time of type segmentation
fault, this means that you are trying to access memory that does
not belong to you. Check if you are not try to access the size
element of an array. This element does not exist!
Some things to think about
When you declare an array, the name of the array is a pointer to the
first element of the array...