C plus plus array

C++ Array

C++ array is a collection of homogeneous data elements in contiguous memory locations. The elements are accessed by their position in the array. This way of accessing is called indexing or sub scripting. The first element is the 0th element. If you declare an array of n elements, then last index is (n-1). Accessing an element whose index is n is an error. A subscript can be any integer expression. For e.g. these are ll valid subscripts:

Foo[17], foo[i+3], foo[a+b+c]

For e.g. int arr[4] –declares an array of 4 integers stored in contiguous memory locations each occupying 2 bytes. Similarly we can have an array of all primitive data types like an array of floating numbers, array of characters, etc., But it is not possible to declare an array that contains some integers, some characters, some double numbers because array can contain only homogeneous data elements.

C++ array can be thought of as a collection of numbered boxes each containing one data item. The number associated with the box is the index of the item. To access a particular item, the index of the box associated with the item must be given. The index must be an integer and indicates the position of the element in the array. The elements of an array are ordered by the index. For e.g.  int arr [4] can be thought of as shown in the following figure.




Declaring arrays in C++

There are two ways to declare arrays in C++ statically and dynamically.

  • Static Arrays
The syntax for declaration of C++ arrays statically is as shown in the following figure.
Data type – tells the compiler the size for each array element.
array_name cab be variable name.
number_of_elements can be an expression that tells the compiler to reserve a specified contiguous memory locations.
For e.g. The following C++ statement declares statically array of 10 integers;
Int my_array[10];
In this example data type is int which tells the compiler that the size for each element is 2 bytes. The name of the array is my_array.  The number 10 appearing between the brackets tells the compiler to reserve 10 contiguous memory locations. The number appearing between the brackets doesn’t have to be a numeric literal. It can be a valid identifier that represents an integer value. For example, a faster way of initializing all the elements of my_array to 100 would be declare an integer variable, set its value to 0, use it to index my_array, then increment the variable by one and repeat the process until all the elements were set to 100.
The problem with array declared statically is that once you’ve declared the array size, it persists for the duration of the program. You have to know ahead of time how many elements you’ll need for storage and reserve.  Memory for arrays declared statically is carved out of the stack.

Assigning Values to the C++ array elements

There are two ways assigning the values to the array element. In the first method, array elements are assigned the values just like any other variable using assignment statements. A value should be assigned to a memory cell in C++. Otherwise, a trash may be present in the memory cell. C++ does not set all numeric memory cells to 0. For e.g. to assign the values to an integer array containing 4 element i.e., int arr[4], we use the following C++ assignment statements:
In the second method, arrays can be declared and initialized at the same time. An array can be initialized in a manner similar to variables. Here, the initial values are given as a list enclosed in curly brackets. As an example, for initializing an array to hold the first few prime numbers is written as follows:
Int primes [] = { 1, 2, 3, 5, 7, 11, 13};
In the above example the array has not been given a size, but the complier will make it large enough to hold the number of elements in the list. Here, array primes would be allocated space for seven elements. If the array is given a size then the size must be greater than or equal to the number of elements in the initialization list. For example:
int primes [10] = {1, 2, 3, 5, 7}
Would reserve space for a ten element array but would only initialize the first five elements.

Accessing C++ array elements

Accessing array elements requires three information’s viz.
  • The starting address of the array or base address.
  • C++ array element number or array index you wish to access.
  • The data type size, in bytes, of the data type stored in the array. The size of the data type, along with the index, will be used to calculate an offset into the array from the address of the first element using the following formula.
Address of array element = base address + (data type size * array index)
Array index
Memory location
Array element
Above table Structure of an array containing 4 elements
Suppose we want to access third element of the array, we need to give index (subscript) as 2(because array index starts with 0) i.e., arr[2]. The address for accessing the third element is calculated as per the above formula.

Address=0010+ (2*2) =0014, where data type size is 2 because it is an integer array and subscript is also 2.

Leave a Reply

Your email address will not be published.