0% found this document useful (0 votes)
6 views1 page

Arrays

An array is a contiguous block of memory that stores multiple elements of the same data type. It allows accessing elements through a single identifier plus an index number. Arrays must be declared with the name, type of elements, and total number of elements. For example, an array called foo that stores 5 integer values would be declared as int foo[5]; where each element is accessed as foo[0] to foo[4].

Uploaded by

icul1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Arrays

An array is a contiguous block of memory that stores multiple elements of the same data type. It allows accessing elements through a single identifier plus an index number. Arrays must be declared with the name, type of elements, and total number of elements. For example, an array called foo that stores 5 integer values would be declared as int foo[5]; where each element is accessed as foo[0] to foo[4].

Uploaded by

icul1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Arrays

An array is a series of elements of the same type placed in contiguous memory locations that can
be individually referenced by adding an index to a unique identifier.

That means that, for example, five values of type int can be declared as an array without having
to declare 5 different variables (each with its own identifier). Instead, using an array, the
five int values are stored in contiguous memory locations, and all five can be accessed using the
same identifier, with the proper index.

For example, an array containing 5 integer values of type int called foo could be represented as:


where each blank panel represents an element of the array. In this case, these are values of
type int. These elements are numbered from 0 to 4, being 0 the first and 4 the last; In C++, the
first element in an array is always numbered with a zero (not a one), no matter its length.

Like a regular variable, an array must be declared before it is used. A typical declaration for an
array in C++ is:

type name [elements];

where type is a valid type (such as int, float...), name is a valid identifier and
the elements field (which is always enclosed in square brackets []), specifies the length of the
array in terms of the number of elements.

Therefore, the foo array, with five elements of type int, can be declared as:
int foo [5];


NOTE: The elements field within square brackets [], representing the number of elements in
the array, must be aconstant expression, since arrays are blocks of static memory whose size
must be determined at compile time, before the program runs.

You might also like