Arrays in C

Arrays

  • Arrays are a fundamental collection type in many programming languages.

  • They serve as the foundation for abstract data types and are used in various algorithms.

  • Implementations vary across languages, affecting behavior and features.

Declaration Syntax

  • Syntax: type[] identifier

  • type: Any data type (string, int, float, bool).

  • Arrays in C are statically typed.

  • Only values matching the declared type can be elements of the array.

  • Contrast with dynamically typed languages like Python (lists can contain mixed types).

  • identifier: Follows variable naming rules.

  • Typically plural to indicate multiple values (e.g., names, ages).

Creation Syntax

  • Differs from initializing a single value in syntax and implementation.

  • Requires the new keyword.

  • Can be done on a single line or over two lines if the array is already declared.

  • Specify the length of the array (maximum number of elements).

  • Length is independent of the data type.

Array Length and Memory

  • Maximum length: An unsigned literal integer (approximately 2,000,000,000).

  • Can find exact maximum length via int32.MaxValue or course materials.

  • Use variables or constants (e.g., daysInWeek, monthsInYear) instead of literal values (magic numbers).

  • Magic numbers indicate poor code quality.

  • Arrays must be initialized before use to avoid compiler errors.

  • Printing an array identifier displays the array's type (e.g., int32) instead of specific values.

Memory Allocation

  • Memory reservation is similar to individual values but scaled up.

  • Memory reserved = (number of bytes per element) * (array length).

  • Example: Array of 20 doubles reserves 160 bytes (since a double uses 8 bytes, 20 * 8 = 160).

  • Arrays start at an address in memory and reserve contiguous space.

  • Arrays can be reassigned with different lengths, overwriting existing values with default values.

  • This reassignment feature is not available in languages like C++.

Default Values

  • Numeric types: 0

  • Characters: Unicode 0

  • Booleans: false

  • Strings: null

Case Study: Bakery Employee Hours

  • Scenario: Gordon's bakery with five employees (Jenny, Simon, Yue, Felipe, Nala) and their hours worked.

  • Example: Jenny (30 hours), Simon (50 hours), Yue (20 hours), Felipe (15 hours), Nala (40 hours).

  • Goal: Calculate the total number of hours worked.

Array Initialization

  • Determine array length and element values.

  • Methods:

    • Initialize during declaration.

    • Initialize each element individually by index.

Initialization During Declaration (Examples)
  1. int[] hours = {30, 50, 20, 15, 40}; (Compiler infers length).

  2. int[] hours = new int[] {30, 50, 20, 15, 40};

  3. int[] hours = new int[5] {30, 50, 20, 15, 40}; (Conventional declaration).

  • All three methods produce the same result.

  • In the third method, the number of elements must match the specified length.

  • Advantage of specifying length: Acts as a safeguard to reduce bugs, especially when the length is well known.

  • Disadvantage: Can generate an error if the length does not match the number of elements when the length is arbitrary.

Individual Element Initialization

  • Access each value using its index (starting from 0 up to array length - 1).

  • Example: hours[0] = 30;

  • Problem: Requires a lot of code for many elements.

Alternative Initialization Methods

  • Initialize values during declaration (as shown above).

  • Use loops (e.g., when reading from a data file).

Brackets

  • In .NET 8, braces {} or square brackets [] can be used to initialize an array.

  • Use square brackets [] in the declaration.

Arrays and Memory

  • Arrays store memory in a block of bytes and use an index to access or store individual elements.

  • Arrays have existed since at least the 1950s.