The next few sections will deal with C's advanced data structures. Arrays are the simplest of these. An array in C is just like an array in any other language: a group of data items of the same type. Let's skip the boring theoretical bit. Here's a small program that copies the first line of the screen to an array of characters.
void main() { unsigned int i; char status_line [40];
for (i = 0; i
First of all I have to say that the header file sys/oric.h contains various Oric-specific definitions (of which this program uses peek()). A note for DOS users: I've used a forward slash (/) as the directory separator. This is perfectly acceptable 1, and has the extra advantage of allowing the same program to be compiled with the Linux version of the compiler 2.
Right, back to the point. This program shows how to define and use arrays (in this case, we define a forty-character array named status_line). You define an array just like any normal variable, but you append to it the number of elements enclosed in square brackets:
Where datatype is the data type of the array's elements, name is the name of the array, and number_of_elements is the price of fish in India 3.
You can access an array element by using the array element operator ([]). In the case of our example, status_line[0] is the array's first element and status_line[39] is the last element. C arrays always start with element 0. Because of this, this array's last element does not have index 40, but 39. Don't forget this, it's a common mistake among people who have just migrated from a more ‘reasonable’ language (like Pascal).
The reason for this will be explained later in this article. As for how you use an array element, (I hope) it's made clear in the example: you can treat it like any ordinary variable. Assign values to it, use it in expressions, etc.
How much space does it take? Simple. You can always use sizeof(status_line) to find out. You'll see it takes 40 bytes of memory. Unlike other languages, C arrays are contiguous and packed: all array elements are grouped together in one large block of memory and there is no unused space between array elements. So, the size of an array is the size of one element times the number of elements in the array.
Another warning: try to avoid using off-range array elements (e.g. status_line[40] is off-range, since the last element is number 39). C performs no range checking (for reasons of speed), and you'll never get a compiler error if you do that. Your program will probably fail in very strange ways, though.
Multi-Dimensional Arrays
Of course, in some cases, you'll want to use arrays of more than one dimension (for example, storing a list of co-ordinates will need a two-dimensional array). It's easy:
Example (a two-dimensional array of co-ordinates in space):
How much space does this one take? It's still the same, of course: the number of elements times the size of one element. In this case, coord_list has a total of 10*2 elements; sizeof(int) is 4 (2 for the 16-bit compiler), so the total size of the array in memory is 10*2*4 bytes.
To address an element of a multi-dimensional array, you have to give all dimensions inside separate square brackets: coord_list[3][0] is an example of how you do it. You might be tempted to say coord_list[3,0]. C won't say anything, but it will be wrong. Remember the comma operator? It evaluates everything, but discards the result of the expression to its left and returns the result of the right-hand side one. So this will be the same as coord_list[0]. You might think that addressing a two-dimensional array by using a single dimension is an error. It's not, in C. I'll make it clear when I explain how C handles arrays internally.
How is it organised in memory? A multi-dimensional array is still contiguous and packed. The sequence of elements in memory is like this:
Sequence of elements in memory:
A[0][0], A[0][1], A[0][2], A[1][0], A[1][1], A[1][2], A[2][0], ..., A[2][2], ... A[9][0], ..., A[9][2]




Comments
Trackback: six pack shortcut
Trackback from http://www.lyrica.com/content/6-charges-tips-developing-six-pack-ab-muscles.Arrays | BedroomLAN
Add new comment