An array is one of the many data structures that Computer Science has developed to manage/organize information
The array is a static structure, i.e., the number of elements in an array is fixed at creation and cannot be changed (unless you destroy the array and create a new one)
(In contrast, the linked list data structure is dynamic: the number of elements can change)
|
|
|
|
|
Problem:
|
Solution:
|
|
|
Important note:
|
|
|
|
|
|
|
Address of B[k] = Base address of array B + k × size of an array element |
The formula is graphically explained as follows:
|
|
B[6] (6, 3, and 5 are constants B[3] B[5] |
Accessing an array element with a constant as array index:
|
Variable definition: int ans; int MyArray[10]; // Array with 10 elements |
B[k] - k is a variable (must be int !) |
Accessing an array element with a variable as array index:
|
Variable definition: int ans; int k; // Assume k has been initialized to some value int MyArray[10]; // Array with 10 elements |
Variable definition: short ans; int k; // Assume k has been initialized to some value short MyArray[10]; // Array with 10 elements |
Note:
|