Arrays are Difficult Beasts


or,

Arrays: Part Two


TOPICS:

  1. How do I store a value into my array ?
  2. How do I retrieve a value from my array ?
  3. How do I index the array ?
  4. When you say array I think loops. Should I ?
  5. What does it mean to say my index is "out of bounds" ?
  6. What happens if I use an index that is out of bounds ?
  7. sizeof() didn't tell me how many elements were in my array, why ?
  8. How do I use sizeof() to find out how many elements are in my array ?
  9. Can I copy one array to another ?
  10. How come the compiler gave me an error message when I tried to copy one array into another one ?


How do I store a value into my array ?

An element of an array can be refered to by the array name and the position the element holds in the array.

For example:
If you have an array: float joe[32]
You can refer to the thirteenth element, joe[12], and assign a value to it, just like you do any other element:

You can also use the postfix and prefix operators on joe[3]:

You can take the address of the 18th element of joe:

You can name your array anything you want. "joe" does just as well as "zippy" or "int_array" or simply the letter "a".

Remember, to access the "Nth" element of joe, you access element "N-1".

See also, related entries on the Array Basics page.

Back to the Topic Index


How do I retrieve a value from my array ?

Every element in an array has an index which represents is position in the array. Remember that the first position starts with zero and you can find anything using the bracket operators: [ and ].

For example:
If you have an array: int joe[665]
You can refer to the thirteenth element, joe[12], and retrieve the value from it just like you do any other element:

Remember, to access the "Nth" element of joe, you access element "N-1".

You can name your array anything you want. "joe" does just as well as "zippy" or "int_array" or something as simple as "a".

See also, related entries on the Array Basics page.

Back to the Topic Index


How do I index the array ?

When you 'index' an array, you are accessing a particular element. This is the same as storing an element into your array, or referencing the value of a particular element.

The Index is the number of the cell of the array element you want to access. That number can be a constant value like '3', or a integer variable.

For Example:

Back to the Topic Index


When you say array I think loops. Should I ?

There are lots of reasons you might connect loops and arrays. Often you want to process the entire array, element by element. Loops were designed for repetitive work.

For example:

int joe[7] = { 0, 1, 2, 3, 4, 5, 6 };
for( i = 0; i = 6; i++ )

What will the output be?

Back to the Topic Index


What does it mean to say my index is "out of bounds"?

The bounds of your array are set when you declare the array.

For example:

When you exceed your array bounds in any direction, you're in trouble. Anything can happen.

Back to the Topic Index


What happens if I use an index that is out of bounds?

Don't do it! If you do, several things may happen (all bad!). For example, your program may stop running and produce a core dump. Or if you print out the value of an array indexed beyond its bounds: just about any value could be printed. There would be no reason to want to do this, but if you see strange values in your output, this is is a possible cause.

This is a perfect exercise. Write a short program and find out what happens when you try to access elements outside your array.

Writing short programs is how you learn what really happens. Features that are 'implementation defined', that differ from compiler to compiler are perfect targets for this sort of short-test-program. If you have a question that I haven't addressed, send it to me, I'll be happy to include it (that's what this is for, anyway). But you can also write a short program to see for yourself. You can always check my answers and see if I gave you the right ones.

Back to the Topic Index


sizeof() didn't tell me how many elements were in my array, why?

the sizeof function doesn't tell you the size of your array, it tells you how much space all the elements in your array take up. Its the number of elements TIMES the size each element takes up.

For an integer array you have to divide by the size of an integer to get the number of elements in your array.

The size of an int isn't constant from machine to machine, so how do I use sizeof() to find out how many elements are in an array?

Back to the Topic Index


How do I use the sizeof() function to find out how many elements are in my array ?

To maintain portability, the best way to use sizeof() to find how many elements are in your array is to take the sizeof your array and divide by the size of your first element.

Given: some array called Zippy
sizeof( Zippy ) / sizeof( Zippy[0] )
where,

Total Size / Element size = Number of Elements

Back to the Topic Index


Can I copy one array to another ?

Sure. You just have to do it element by element. You can copy a smaller array into a bigger one. You can copy a part of a bigger array into a smaller one. Just be sure to do it One Element At A Time.

Back to the Topic Index


How come the compiler gave me an error message when I tried to copy one array into another one ?

Chances are you made a simple, but common mistake. You can't copy arrays directly from one to the other. You have to copy them element by element.

Given Two arrays:

You can't just copy a to b like they are regular variables:
ILLEGAL:

You have to copy one array to another, element by element. How do you access an array element by element?

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).