Page 30 - Data Structures Interactive Book
P. 30
Chapter 3
3.1 Introduction to Arrays
Arrays are one of the most fundamental data structures in C++. They provide a way to
store multiple elements of the same type in a contiguous block of memory. Each element in
an array is identified by an index, which allows constant-time access. Arrays are widely used
because they are simple, efficient, and form the basis for more advanced structures such as
matrices, strings, and hash tables.
3.1.1 Definition and Characteristics
An array is a collection of elements arranged sequentially in memory. All elements
must be of the same data type, and the size of the array must be specified at the time of
declaration. Arrays allow random access, meaning any element can be accessed directly using
its index. However, the size of an array is fixed, which can be a limitation when dealing with
dynamic data.
3.1.2 Declaration and Initialization
Arrays in C++ can be declared by specifying the data type, name, and size. Initialization
can be done at the time of declaration or later in the program.
Example:
This declares an array of five integers and initializes them with values.
3.1.3 Memory Representation
Arrays are stored in contiguous memory locations. The index of an array element
corresponds to its offset from the base address. For example, if the base address of
numbers[0] is 1000, then numbers[1] will be at 1004 (assuming 4 bytes per integer). This
contiguous arrangement enables efficient access but also means that resizing arrays is not
straightforward.
30

