An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). 数组是元素的集合,存储在连续的内存空间,类型一样,有利于计算每个元素的位置,在基数上增加偏移即可。
Types of indexing in array:
- 0 (zero-based indexing): The first element of the array is indexed by subscript of 0 下标从0开始的
- 1 (one-based indexing): The first element of the array is indexed by subscript of 1 下标从1开始的
- n (n-based indexing): The base index of an array can be freely chosen. Usually programming languages allowing n-based indexing also allow negative index values and other scalar data types like enumerations, or characters may be used as an array index.
Advantages of using arrays:
- Arrays allow random access of elements. This makes accessing elements by position faster. array可以随机访问,
- Arrays have better cache locality that can make a pretty big difference in performance.
Examples –
// A character array in C/C++/Java char arr1[] = {'g', 'e', 'e', 'k', 's'}; // An Integer array in C/C++/Java int arr2[] = {10, 20, 30, 40, 50}; // Item at i'th index in array is typically accessed // as "arr[i]". For example arr1[0] gives us 'g' // and arr2[3] gives us 40.
Arrays in C/C++
An array is collection of items stored at continuous memory locations.
Why do we need arrays?
We can use normal variables (v1, v2, v3, ..) when we have small number of objects, but if we want to store large number of instances, it becomes difficult to manage them with normal variables. The idea of array is to represent many instances in one variable.
Array declaration in C/C++:
We can declare an array by specifying its type and size or by initializing it or by both.
- Array declaration by specifying size
// Array declaration by specifying size
int arr1[10];
// With recent C/C++ versions, we can also
// declare an array of user specified size
int n = 10;
int arr2[n];
- Array declaration by initializing elements
// Array declaration by initializing elements
int arr[] = { 10, 20, 30, 40 }
// Compiler creates an array of size 4.
// above is same as "int arr[4] = {10, 20, 30, 40}"
- Array declaration by specifying size and initializing elements
// Array declaration by specifying size and initializing
// elements
int arr[6] = { 10, 20, 30, 40 }
// Compiler creates an array of size 6, initializes first
// 4 elements as specified by user and rest two elements as 0.
// above is same as "int arr[] = {10, 20, 30, 40, 0, 0}"
Facts about Array in C/C++:
- Accessing Array Elements:
Array elements are accessed by using an integer index. Array index starts with 0 and goes till size of array minus 1.Following are few examples.
#include <stdio.h>
int main()
{
int arr[5];
arr[0] = 5;
arr[2] = -10;
arr[3 / 2] = 2; // this is same as arr[1] = 2
arr[3] = arr[0];
printf("%d %d %d %d", arr[0], arr[1], arr[2], arr[3]);
return 0;
}
- Output:
5 2 -10 5
- No Index Out of bound Checking:
There is no index out of bound checking in C, for example the following program compiles fine but may produce unexpected output when run.
// This C program compiles fine
// as index out of bound
// is not checked in C.
#include <stdio.h>
int main()
{
int arr[2];
printf("%d ", arr[3]);
printf("%d ", arr[-2]);
return 0;
}
- Output:
2008101287 4195777
- Also, In C, it i