Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Arrays

C++ Arrays

C++ supportsarraysdata structure, which can store a sequential collection of elements of a fixed size and the same type. An array is used to store a series of data, but it is often thought of as a sequence of variables of the same type.

Declaring an array does not declare individual separate variables, such as number0, number1, ..., number99, but rather declares a single array variable, such as numbers, and then uses numbers[0], numbers[1], ..., numbers[99] to represent the individual variables. Specific elements in the array can be accessed by index.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element, and the highest address corresponds to the last element.

Declaring Arrays

To declare an array in C++, you need to specify the type of the elements and the number of elements, as shown below:



type arrayName [ arraySize ];

This is called a one-dimensional array.arraySize must be an integer constant greater than zero,type can be any valid C++ data type. For example, to declare an array of type double containing 10 elements balance, the declaration statement is as follows:



double balance[10];

Now balance is a usable array that can hold 10 numbers of type double.

Initializing Arrays

In C++, you can initialize an array element by element, or use an initialization statement, as shown below:



double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};

The number of values between the braces { } cannot be greater than the number of elements we specified in the square brackets [ ] during the array declaration.

If you omit the size of the array, the size of the array becomes the number of elements at initialization. Therefore, if:



double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};

you will create an array that is exactly the same as the one created in the previous example. The following is an example of assigning a value to an element in an array:



balance[4] = 50.0;

The above statement assigns the value 50.0 to the fifth element of the array. All arrays use 0 as the index of their first element, also known as the base index; the last index of the array is the total size of the array minus 1. The following is a graphical representation of the array discussed above:

Array representation

Accessing Array Elements

Array elements can be accessed by the array name plus an index. The element's index is placed in square brackets, following the array name. For example:



double salary = balance[9];

The statement above will assign the value of the 10th element in the array to the salary variable. The following example uses the three concepts above: declaring an array, assigning to an array, and accessing an array:

Example

#include

<

iostream

using

namespace

std

;

#include

<

iomanip

using

std

::

setw

;

int

main

(

)

{

int

n

[

10

]

;

// n is an array containing 10 integers

// initialize array elements

for

(

int

i

=

0

;

i

<

10

;

i

++

)

{

n

[

i

]

=

i

100

;

// set element i to i + 100

}

cout

<<

"

Element

"

<<

setw

(

13

)

<<

"

Value

"

<<

endl

;

// output the value of each element in the array

for

(

int

j

=

0

;

j

<

10

;

j

++

)

{

cout

<<

setw

(

7

)

<<

j

<<

setw

(

13

)

<<

n

[

j

]

<<

endl

;

}

return

0

;

}

The program above uses the setw() function to format the output. When the code above is compiled and executed, it produces the following result:



Element        Value

      0          100

      1          101

      2          102

      3          103

      4          104

      5          105

      6          106

      7          107

      8          108

      9          109

Detailed Explanation of Arrays in C++

In C++, arrays are very important, and we need to understand more details about them. The following lists some important array-related concepts that C++ programmers must be clear about:

Concept Description
Multidimensional Arrays C++ supports multidimensional arrays. The simplest form of a multidimensional array is a two-dimensional array.
Pointers to Arrays You can generate a pointer to the first element in the array by specifying the array name without an index.
Passing Arrays to Functions You can pass a pointer to an array to a function by specifying the array name without an index.
Returning Arrays from Functions C++ allows returning arrays from functions.