INTRODUCTION TO ARRAYS
Welcome to our discussion on arrays and their memory
representation! In this chapter, we will explore how computer memory
is organized and how arrays use that organization to store data
efficiently.
To begin, let's define an array as a collection of elements, all of the
same data type, stored in contiguous memory locations. Each element
can be individually accessed by its index, which is a unique identifier for
its position in the array. The first element has an index of 0, the second
element has an index of 1, and so on.
Now, let's examine how arrays are stored in memory. Consider the
following array of integers:
int arr[5] = {1, 2, 3, 4, 5};
This array contains five integers, each taking up 4 bytes of memory. The
array is stored in contiguous memory locations, as shown in the
diagram below:
+-------+-------+-------+-------+-------+
| arr[0]| arr[1]| arr[2]| arr[3]| arr[4]|
+-------+-------+-------+-------+-------+
, | 1 | 2 | 3 | 4 | 5 |
+-------+-------+-------+-------+-------+
DECLARATION OF ARRAYS
An array can be declared using the following syntax:
dataType arrayName[arraySize];
where dataType is the type of data that the array will
hold, arrayName is the name given to the array, and arraySize is the
number of elements the array can hold.
For example, to declare an array of integers with a size of 5, the
following syntax can be used:
int myArray[5];
Initialization can be done using the following syntax:
arrayName[index] = value;
where index is the position in the array where the value will be stored,
and value is the data to be stored.
For example:
myArray[0] = 10; myArray[1] = 20; myArray[2] = 30; myArray[3] =
40; myArray[4] = 50;
Limitations and Drawbacks of Static Arrays
Fixed size: The size of a static array must be specified at the time
of declaration, it cannot be changed later. This means that if the
Welcome to our discussion on arrays and their memory
representation! In this chapter, we will explore how computer memory
is organized and how arrays use that organization to store data
efficiently.
To begin, let's define an array as a collection of elements, all of the
same data type, stored in contiguous memory locations. Each element
can be individually accessed by its index, which is a unique identifier for
its position in the array. The first element has an index of 0, the second
element has an index of 1, and so on.
Now, let's examine how arrays are stored in memory. Consider the
following array of integers:
int arr[5] = {1, 2, 3, 4, 5};
This array contains five integers, each taking up 4 bytes of memory. The
array is stored in contiguous memory locations, as shown in the
diagram below:
+-------+-------+-------+-------+-------+
| arr[0]| arr[1]| arr[2]| arr[3]| arr[4]|
+-------+-------+-------+-------+-------+
, | 1 | 2 | 3 | 4 | 5 |
+-------+-------+-------+-------+-------+
DECLARATION OF ARRAYS
An array can be declared using the following syntax:
dataType arrayName[arraySize];
where dataType is the type of data that the array will
hold, arrayName is the name given to the array, and arraySize is the
number of elements the array can hold.
For example, to declare an array of integers with a size of 5, the
following syntax can be used:
int myArray[5];
Initialization can be done using the following syntax:
arrayName[index] = value;
where index is the position in the array where the value will be stored,
and value is the data to be stored.
For example:
myArray[0] = 10; myArray[1] = 20; myArray[2] = 30; myArray[3] =
40; myArray[4] = 50;
Limitations and Drawbacks of Static Arrays
Fixed size: The size of a static array must be specified at the time
of declaration, it cannot be changed later. This means that if the