An array is a very basic data structure. It can be used to implement another data structure.
Here are the few properties of the array -
Store elements in the contiguous memory.
x = [10, 11, 32, 0, -1, 22];
suppose &x[0] = 0X0234
then &x[1] will be 0X0235
&x[2] will be 0X0236
and so on...
Note: & (ampersand) denotes as the memory address
Store elements of same type like if it is defined as integer then it can only stores integers.
int x[10];
// Now try to add x[0] = 'c';
// it will throw an error
Can possible random access. Note in practical array index start from 0.
x = [1, 4, 2, 5, 6, 2];
print(x[0]); // output 1
print(x[3]) //output 5
print(x[100]) // throw an error because element not found
Insertion at the end takes O(1) time and deletion at the end takes O(n) time.
Insertion takes O(n) time if it’s not the end element.
If the size of an array is N then it can be indexed from 0…N-1 or 1…N (theoretically).
Total size of the array depends on the length of the array and its data type
int x[10];
/**
total size = size of int * 10
= 4 bytes * 10
= 40 bytes
**/
char name[100];
/**
total size = size of char * 100
= 1 bytes * 10
= 100 bytes
**/
int x[10];
// The valid range of values for the example above is 0 through 9. The first element of the array is accessed using index value 0 and the last element of the array is accessed using index value 9.
// An overflow error occurs when one attempts to access the example array using an index value greater than 9.
// An underflow error occurs when one attempts to access the example array using an index value less than 0.