Skip to content

Arrays, Strings and Structures

Objective

At the end of this chapter, you should be able to read, write, compile and run simple C programs with the following features:

  • Arrays with additional understanding on:
    • Interoperability with pointers
  • Strings with additional understanding on:
    • String functions
  • Structures with additional understanding on:
    • Pointers and arrows

Preliminary

So far, what we have learnt are primitive data types. The next part is to combine these primitive data types into a more complex data types. There are two basic ways of combining data in C.

  1. Collections of the same type of data (i.e., homogeneous).
  2. Collections of different types of data (i.e., heterogeneous).

The first is solved using arrays and typically involve a large collection. As the number of elements involved is usually large, retrieval of elements is done using numbers corresponding to the index.

On the other hand, the second is solved using struct and typically involve a small collection. Hence, we can give a "name" to the elements for easy retrieval.

C Array

Unlike other modern languages like JavaScript or Python, C array has a fixed size. Once it is declared with a certain size, the size cannot increase. Unfortunately, unlike Java, the size in C must be a constant and cannot come from a variable. So you really need to know how big you want to store things because it cannot depend on user input.

To make matter worse, C does not perform any checking on retrieval of elements from an array. So you can declare an array of size 30, but try to retrieve the 50th element! This may or may not cause an error depending on whether the location is valid memory location allocated to the program or not.

Comparison

In JavaScript, arrays are created using [elem, elem, elem, ...]. In C, they are initialised using {elem, elem, elem, ...} instead. However, unlike in C, JavaScript arrays auto-expand. In particular, given an array of arr size 2, the statement arr[10] = 0; will actually automatically expand the array to size 11 (remember, the first index is 0).

As for struct, it is similar to JavaScript object but without the prototypical inheritance available in JavaScript. Both are mappings from name to values. But in the case of JavaScript, new names can be inserted to an existing object while C struct must always follow the format defined in the definition of the struct.

Python does not have arrays but it has list. The syntax is [elem, elem, elem, ...]. In C, they are initialised using {elem, elem, elem, ...} instead. While the underlying implementation in CPython maps list to array, this is hidden from the programmers.

As for struct, it is similar to Python dictionary. However, retrieval of element has a syntax similar to Python class. In any case, there are no methods available for struct unlike both dictionary and user-defined class. But they are both still mapping from name (key for dictionary or attribute for class) to values. Unfortunately, you cannot insert new name in C struct.