CampusLearn
← CSC 112 Structured Programming

Composite Data Types: Arrays and Structures

Notes

Understanding Composite Data Types Primitive data types such as int, float, and char represent individual, atomic values. However, real-world software engineering demands modeling collections and composite entities. Structured programming provides composite or derived data types, notably arrays and structures, to group related data elements logically and manage memory efficiently. Single and Multidimensional Arrays An array is a homogeneous, fixed-size data structure that stores elements of the identical data type in contiguous memory locations. Individual elements are accessed in constant time using a zero-based integer index. A one-dimensional array models linear lists, while multidimensional arrays, such as matrices, model tables with rows and columns. In C, the array identifier represents a pointer to its first memory address, highlighting the strong intrinsic relationship between arrays and pointer arithmetic. Structured Records Using Structs While arrays store homogeneous elements, a structure or struct represents a heterogeneous composite data type. It permits bundling multiple fields of varying types under a single identifier. A struct creates a user-defined blueprint representing real-world records. Members within a structure instance are accessed using the dot operator, or through the arrow operator when manipulating a pointer to a struct. Memory Layout Considerations Because arrays allocate contiguous memory, traversing them sequentially maximizes cache performance. For structs, compilers may introduce padding bytes to align member data with system word boundaries, an architectural detail essential for low-level systems programming. Practical example Consider managing fleet operations for a Nairobi Matatu Savings and Credit Cooperative Society (SACCO) operating along the Thika Superhighway. The system must track passenger counts, fuel expenses, and daily revenues across multiple vehicles. A struct named Matatu is defined with fields: a character array for the registration plate such as KDA 123X, an integer for passenger capacity, a float for collected fare revenue, and a float for fuel costs. To manage the entire fleet, an array of Matatu structures is instantiated, holding records for thirty distinct vehicles. A function processes this array using a for loop, iterating through each index from zero to twenty-nine. In each iteration, it reads the collected revenue, deducts fuel expenses, and stores the net profit in a SACCO summary variable. By pairing structs with arrays, the cooperative maintains clear, type-safe records for each public service vehicle while processing fleet-wide metrics with minimal code complexity.

Quiz

1. What distinguishes a structure (struct) from an array in structured programming?

2. Which operator is used to access a member of a structure through a pointer variable?

3. How are elements stored in a one-dimensional array in the C language?

Sign in to submit and securely grade your quiz.