Arrays

Overview

A ragged array is a term for an array of pointers which point to objects with a different memory length. One commmon example is an array of pointers to strings, where each string has a variable number of characters.

A array of pointers to some data type (e.g. an array of pointers to strings) is also a type of Iliffe vector. You have the advantage of being able to use NULL to signify the end of the array (no other data

Weird Syntax

It is a little known and weird fact, but arrays can be wrriten as a[5] or 5[a], it does not matter. However, using the syntax 6[a] serves no useful purpose, and confuses people to no-end, so I don’t recommend using it!

Arrays As Formal Parameters

You have to give all but the left-most dimension in a formal parameter which is an array. Hence you cannot pass multi-dimensional arrays into a function. If you think about it, this makes sense, given that C adds no run-time calculations to array operations. For this reason, there is no possible way the compiler can calculate the memory offset (remember, an array is just a contiguous, linear block of memory) for say, myArray[3][2] without knowing the sizes of the array.

Dynamic Arrays

Dynamic arrays are arrays whos size is determined at runtime. Programmers use them when we don’t know the size of the array in advance (i.e., we can’t write a hard-coded integer into the code, but instead we have to use a variable such as x).

Dynamic arrays are not allowed in ANSI C, all arrays have to be static. Infact, even this will give you an error!

what! Fortunately, C99 has come to the rescue, and allows you to create dynamic arrays on the stack. You can also create dynamic arrays (in all versions of C) on the heap by using the malloc() family of functions.