C++:Creating an Array

From Progzoo
Jump to navigation Jump to search

You can declare an array, you can reserve space for the array, you can create an array literal.

Create an Array

You use square brackets to indicate that an array is required.

int x[5];
string s[2];

You must specify the size of the array so that the compiler can reserve sufficient space. The array elements are not initialised - they will contain junk data unless you set them explicitly.


[Font] [Default] [Show] [Resize] [History] [Profile]

Create an Array Literal

You can initialise the array to a specific list of values:

Notice that the declaration x[] need not include the size of the array.


[Font] [Default] [Show] [Resize] [History] [Profile]

Create a two dimensional Array Literal

You can initialise the array to a specific list of values:

Notice the declaration x[][3] - the size of all but the first dimension must be given.


[Font] [Default] [Show] [Resize] [History] [Profile]