4.5 Arrays and Structures
4.5 Arrays and Structures
Introduction to Data Aggregation
Simple variables store single data items. To solve real-world problems, we often need to handle collections of related data. This unit introduces two fundamental composite data types in C: Arrays and Structures. Arrays allow the storage of multiple elements of the same data type in contiguous memory, ideal for lists and matrices. Structures allow the bundling of multiple elements of potentially different data types under a single name, ideal for modeling real-world entities like a student record or a book. Mastering these constructs is crucial for organizing and manipulating complex data efficiently.
1. Arrays
1.1 Definition and Concept
Definition: An array is a fixed-size, contiguous collection of elements of the same data type. All elements share a common name, and each individual element is accessed by its numerical index (position).
Key Characteristics:
Homogeneous: All elements must be of the same type (e.g., all
int, allfloat).Contiguous Memory: Elements are stored in consecutive memory locations.
Fixed Size: The size (number of elements) must be known at compile time and cannot be changed during runtime in standard C.
Random Access: Any element can be accessed directly in constant time using its index.
Syntax for Declaration:
data_type array_name[size];data_type: The type of each element (e.g.,int,float,char).array_name: A valid identifier.size: A positive integer constant specifying the number of elements.Example:
int marks[50];declares an array namedmarksthat can hold 50 integers.
1.2 Array Initialization
Arrays can be initialized at the time of declaration.
Full Initialization:
Partial Initialization:
Remaining elements are automatically set to zero (for numeric types) or
NULL(for pointers).Initialization without Specifying Size:
Zero Initialization:
1.3 Accessing and Processing Array Elements
Indexing: Array elements are accessed using the array name followed by the index in square brackets
[].Indexing starts from 0. The first element is
array_name[0], the last isarray_name[size-1].Example:
marks[0] = 95;assigns 95 to the first element.
Looping Through an Array: Processing arrays almost always involves loops.
1.4 Passing Arrays to Functions
Arrays are passed to functions by reference, not by value. When an array name is used as an argument, what is actually passed is the base address (address of the first element) of the array.
Syntax in Function:
The
[]in the parameter indicates an array. The size inside[]can be omitted.Crucially, you must also pass the size of the array as a separate parameter, because the function receiving the array has no built-in way to know its size.
Example:
1.5 Multidimensional Arrays
An array of arrays. The most common is the two-dimensional (2D) array, which can be visualized as a table with rows and columns.
Declaration:
Example:
int matrix[3][4];declares a 2D array with 3 rows and 4 columns (total 12 integers).
Initialization:
Accessing Elements: Use two indices:
array_name[row_index][column_index].Processing with Nested Loops:
Passing to Functions: Similar to 1D arrays, but the number of columns must be specified.
2. Structures
2.1 Definition and Concept
Definition: A structure (
struct) is a user-defined data type that groups together variables of different data types under a single name. These variables are called members.Purpose: To represent a real-world entity that has multiple attributes. (e.g., a
Studenthasroll_no,name,marks).Syntax for Declaration:
Note the semicolon (
;) after the closing brace.Example:
This defines a new type called
struct Student. No memory is allocated yet.
2.2 Structure Variable Declaration and Initialization
Declaring Structure Variables:
Initializing Structure Variables:
2.3 Accessing Structure Members
Members of a structure variable are accessed using the dot (.) operator.
2.4 Array of Structures
One of the most powerful combinations: an array where each element is a structure. Ideal for storing records of multiple entities.
2.5 Pointers to Structures
A pointer can point to a structure variable. This is efficient for passing large structures to functions (avoids copying the entire structure) and for dynamic memory allocation.
Declaring a Structure Pointer:
Accessing Members via Pointer:
Using the arrow operator (
->) is the standard and cleaner method.Using the dereference and dot operator
(*ptr).memberis equivalent but less common.
Passing Structure Pointers to Functions (Call by Reference for Structures):
2.6 Nested Structures
A structure can have another structure as a member.
2.7 Typedef with Structures
The typedef keyword creates an alias (a new name) for a data type, which can simplify structure declarations.
3. Comparison: Arrays vs Structures
Feature
Array
Structure (struct)
Data Type
Homogeneous (all elements same type)
Heterogeneous (members can be different types)
Element Access
By numeric index (arr[0])
By member name (student.roll)
Memory Allocation
Contiguous for all elements
Contiguous, but may have padding between members
Size
Fixed at compile time
Fixed at compile time (sum of member sizes + padding)
Purpose
Store a collection of similar items (list)
Store a collection of related but different attributes of an entity (record)
Passing to Func.
By reference (base address)
Can be by value (copies entire struct) or by reference (using pointer)
Conclusion: Arrays provide powerful, efficient storage for sequences of uniform data, while structures offer the flexibility to model complex, composite data types. Combining them—through arrays of structures or structures containing arrays—allows programmers to build sophisticated data models that closely mirror real-world information, forming the backbone of many applications, from simple record-keeping systems to complex database representations.
Last updated