Reworded some of the text for C++ arrays (#24477)

This commit is contained in:
Stephen James
2018-11-19 05:06:07 -06:00
committed by Aditya
parent 61cd176a41
commit 0216725df6

View File

@ -2,8 +2,8 @@
title: C++ Arrays title: C++ Arrays
--- ---
## What are Arrays? ## What is an Array?
An array is a series of elements of the same data type which are stored in contiguous memory locations and can be referenced individually. An array is a series of elements of the same data type which are stored in contiguous memory locations and can be referenced as a whole or individually.
Declaration: Declaration:
dataType arrayName[arraySize]; dataType arrayName[arraySize];
@ -21,6 +21,7 @@ int numbers [5] = {1, 2, 3, 4, 5};
//Initialization with no values: //Initialization with no values:
int numbers [5] = {}; int numbers [5] = {};
//In this case the elements in the array are of indeterminate value
//Initialization with fewer values than elements: //Initialization with fewer values than elements:
int numbers [5] = {6}; int numbers [5] = {6};
@ -30,23 +31,23 @@ int numbers [5] = {6};
//Initialization with declaration: //Initialization with declaration:
int numbers [] = {1, 2, 3, 4, 5}; int numbers [] = {1, 2, 3, 4, 5};
//Note that here the number of values defines the size of the array. //Note that here the number of values defines the size of the array.
//In the examples above, the size was fixed beforehand //In the examples above, the size was fixed at compile time
``` ```
## Types Of Arrays ## Types Of Arrays
There are two types of arrays based on the way we declare it. There are two types of arrays based on way the array is declared.
**1**. Static array: **1**. Static array:
Those arrays whose size is defined before compile time like in the examples above, are called static arrays. In these arrays we can't change their size once they are declared. Those arrays whose size is defined before compile time like in the examples above, are called static arrays. In these arrays we can't change their size once they are declared.
**2**. Dynamic array: **2**. Dynamic array:
Dynamic arrays are those arrays, whose size is not known at compile time and we can define their size at run time. These arrays are created by using **new** keyword and when done with that array we can delete that array by using the **delete** keyword. Dynamic arrays are those arrays, whose size is not known at compile time and we can define their size at run time. These arrays are created by using **new** keyword and when done with that array we can delete that array by using the **delete** keyword. Specifically, the use of the `delete[]` syntax.
### Access: ### Access:
Elements from an array can be accessed via reference of their position in the array. (Start counting from 0). Elements of an array are accessed using their index. The index of the first element in the array is zero and the second element's index is 1 and so on. You can think of the index of an element as the unit "distance" from the beginning of the array, that is the first element is 0 units from the start.
Example: Examples using the number array from above:
```C++ ```C++
x = numbers[0]; // = 1. [0] == first position int x = numbers[0]; // x is assigned 1. index 0 is the first position
numbers[2] = 55; // Sets the third position (3) to the new number 55 numbers[2] = 55; // Sets the third position (index 2) to the new value 55
//numbers[] is now: {1, 2, 55, 4, 5} //numbers[] is now: {1, 2, 55, 4, 5}
``` ```