C Programming/Arrays

Objective

 * Learn about arrays and how to use them.
 * Declaring arrays.
 * Initializing arrays.
 * Passing arrays to functions.

Introduction
An array is a series of contiguously allocated variables of the same type. Let us start with an analogy. Suppose that you have a cupboard in your house. You would like to keep books on each shelf of the cupboard. It would be nice to keep books related to one subject on one rack, books of another subject on another rack. A rack is an array. A rack is used to store books of the same subject. Similarly, an array can store data of the same type. When you declare an array, you must specify the type of data this array will hold. Every element of the array must be of that data type.

The syntax for declaring an array is:

For example: Is an array of integers, with size 5, called x. Similarly, is an array of floating point numbers, with size 6, called y.

To access the elements of an array, you must use array notation. The first element of an array is at position 0, the second at position 1, etc. The last element of an array is at position size - 1.

As you can see, once you have selected an element using array notation, you can manipulate that element as you would any variable. Here is a more complex example:

Array Initialization I
When an array is declared, it is initially 'empty'; it does not contain any values. We can initialise an array as follows:

is called an array-initialization block. When we use an array initialization block, we do not need to specify the size of the array. The following is equivalent:

When have created an array with 5 elements. The first element is 5, the second is 7, etc.

Array Initialization II
Let us look at a small code snippet that prints the number of days per month. OUTPUT Month 1 has 31 days. Month 2 has 28 days. Month 3 has 31 days. Month 4 has 30 days. Month 5 has 31 days. Month 6 has 30 days. Month 7 has 31 days. Month 8 has 31 days. Month 9 has 30 days. Month 10 has 31 days. Month 11 has 30 days. Month 12 has 31 days.

(You may not have seen  before. It is a preprocessor instruction that defines a constant called   and assigns it the value  .)

If you lack faith in your ability to count, we can let the computer give us the size of an array, by using. Just replace with

Variable-length array
A variable-length array, also called variable-sized or runtime-sized, is an array data structure whose length is determined at run time (instead of at compile time). In C99, VLAs (Variable-Length Arrays, as they are called) are a mandatory feature, that then became optional in later standards. Note that the major compilers all allow VLAs.

Assigning Array Values
We can assign values to array members by using array index.

Thus the array  will contain the elements 1, 2, 3, 4, and 5.

Passing Arrays to a Function
Arrays cannot be passed to functions. If an array name is used as an argument in a function call, the address of the first element of the array is passed. The function can access the array through that address.

Suppose we want to write a function that returns the sum of elements of the array.

Note that the array name  is used as an argument to the function. The function is expecting an array because its parameter is defined as. What is actually passed is the address of. The function then can access the array using array notation.

How are arrays stored in memory?
When we declare an array, space is reserved in the memory of the computer for the array. The elements of the array are stored in these memory locations. The important thing about arrays is that array elements are always stored in consecutive memory locations. We can verify this fact by printing the memory addresses of the elements. (Just like every person has a street address, every location in the memory has a memory address, usually a number, by which it can be uniquely identified.) OUTPUT Address of a[0] : ffe2 Address of a[1] : ffe4 Address of a[2] : ffe6 Address of a[3] : ffe8 Address of a[4] : ffea Address of a[5] : ffec Address of a[6] : ffee Address of a[7] : fff0 Address of a[8] : fff2 Address of a[9] : fff4

As we can see from the output, the elements are stored at ffe2, ffe4, ffe6, etc. You might be wondering why the numbers are not consecutive. The reason is very simple. The size of the  data type in C is at least 2 bytes (depending on the implementation). In this example it is 2 bytes wide, so  will be stored at ffe2, ffe3,   will be stored at ffe4, ffe5,   will be stored at ffe6, ffe7 and so on.

If instead we declared an array of s (which usually take around 4 bytes each), we will find   will be stored at ffe2, ffe3, ffe4, ffe5,   will be stored at ffe6, ffe7, ffe8, ffe9 and so on. Note that you may see entirely different numbers that represent the address locations.

As you can see, the elements are consecutive. This concludes the lesson on arrays.

The way arrays are placed in memory is so that the name of the array is actually the address of the lowest element in memory, so that higher elements are accessed by doing addition, as may be intuitive. However, a confusing part of this is that the stack grows downwards. What is meant by this is that the stack pointer (sp/esp/rsp - for accessing 16/32/64 its repectively) points to the top of the stack (often the top of the memory) allocated to the program, unless you use special settings (which you shouldn't), or you are on a special system. When you allocate to the stack, variables are allocated going downwards - so when you push (add) a value (or variable) onto the stack, sp/esp/rsp decreases to be pointing to available unused memory, and when you pop (remove) a value (or variable), esp increases to be pointing to the memory above the variable you popped. When you allocate an array, sp/esp/rsp is decreased by that array's size, and the name for the array is saved as it's offset from the frame of course. This means that, while allocated backwards, because it is allocated all at once, the array is accessed upwards. Note that the reason for the direction of stack growth is dynamic memory: dynamic memory, or heap memory, must grow upwards, as otherwise increases/decreases would have to apply to the start of the chunks of memory. Noting this, having both segments grow towards each other is the only safe way to properly allocate memory to actually maximise availability, otherwise there would be a split, making programs that utilise one more than the average suffer due to lack of available memory.

Note: In terms of actual, physical addresses, things get more complicated due to paging, segments and something called a GDT. However, there are literally 2 things that require knowledge about these things: writing an OS and writing drivers, which is part of writing an OS.

Assignments



 * Previous Lesson: Functions
 * Next Lesson: Pointers
 * Course Home Page