/ | ||||
(C++11) | ||||
(C++11) |
(C++11) | ||||
(C++20) | ||||
(C++20) |
(C++11) | ||||
expression |
pointer |
specifier | ||||
specifier (C++11) | ||||
specifier (C++11) |
(C++11) | ||||
(C++11) |
(C++11) | ||||
(C++11) |
Specifiers | ||||
function specifier | ||||
function specifier | ||||
(C++20) | ||||
/struct | ||||
volatile | ||||
(C++26) | ||||
(C++11) |
Declarators | ||||
Block declarations | ||||
→ (C++17) | ||||
(C++11) | ||||
declaration | ||||
directive | ||||
declaration (C++11) | ||||
declaration | ||||
(C++11) | ||||
Other declarations | ||||
(C++11) | ||||
(C++11) | ||||
Declares an object of array type.
Syntax Assignment Array-to-pointer decay Multidimensional arrays Arrays of unknown bound Array rvalues Defect reports See also |
An array declaration is any simple declaration whose declarator has the form
noptr-declarator expr (optional) attr (optional) | |||||||||
noptr-declarator | - | any valid declarator, but if it begins with , , or , it has to be surrounded by parentheses (otherwise the whole declarator is treated as a or ). |
expr | - | an (until C++14)a of type (since C++14), which evaluates to a value greater than zero |
attr | - | (since C++11) list of |
A declaration of the form T a [ N ] ; , declares a as an array object that consists of N contiguously allocated objects of type T . The elements of an array are numbered 0 , …, N - 1 , and may be accessed with the subscript operator [] , as in a [ 0 ] , …, a [ N - 1 ] .
Arrays can be constructed from any fundamental type (except void ), pointers , pointers to members , classes , enumerations , or from other arrays of known bound (in which case the array is said to be multi-dimensional). In other words, only object types except for array types of unknown bound can be element types of array types. Array types of incomplete element type are also incomplete types.
The possibly (since C++20) specifier can be used as array element type in the declaration of a pointer or reference to array, which deduces the element type from the initializer or the function argument(since C++14), e.g. auto (*p)[42] = &a; is valid if a is an lvalue of type int[42]. | (since C++11) |
There are no arrays of references or arrays of functions.
Applying cv-qualifiers to an array type (through typedef or template type manipulation) applies the qualifiers to the element type, but any array type whose elements are of cv-qualified type is considered to have the same cv-qualification.
When used with new[]-expression , the size of an array may be zero; such an array has no elements:
Objects of array type cannot be modified as a whole: even though they are lvalues (e.g. an address of array can be taken), they cannot appear on the left hand side of an assignment operator:
There is an implicit conversion from lvalues and rvalues of array type to rvalues of pointer type: it constructs a pointer to the first element of an array. This conversion is used whenever arrays appear in context where arrays are not expected, but pointers are:
When the element type of an array is another array, it is said that the array is multidimensional:
Note that when array-to-pointer decay is applied, a multidimensional array is converted to a pointer to its first element (e.g., a pointer to its first row or to its first plane): array-to-pointer decay is applied only once.
If expr is omitted in the declaration of an array, the type declared is "array of unknown bound of T", which is a kind of incomplete type , except when used in a declaration with an aggregate initializer :
Because array elements cannot be arrays of unknown bound, multidimensional arrays cannot have unknown bound in a dimension other than the first:
If there is a preceding declaration of the entity in the same scope in which the bound was specified, an omitted array bound is taken to be the same as in that earlier declaration, and similarly for the definition of a static data member of a class:
References and pointers to arrays of unknown bound can be formed, but cannot (until C++20) and can (since C++20) be initialized or assigned from arrays and pointers to arrays of known bound. Note that in the C programming language, pointers to arrays of unknown bound are compatible with pointers to arrays of known bound and are thus convertible and assignable in both directions.
Pointers to arrays of unknown bound cannot participate in pointer arithmetic and cannot be used on the left of the subscript operator , but can be dereferenced.
Although arrays cannot be returned from functions by value and cannot be targets of most cast expressions, array prvalues may be formed by using a type alias to construct an array temporary using brace-initialized functional cast .
Like class prvalues, array prvalues convert to xvalues by when evaluated. | (since C++17) |
Array xvalues may be formed directly by accessing an array member of a class rvalue or by using std::move or another cast or function call that returns an rvalue reference.
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
C++98 | a pointer or reference to an array of unknown bound could not be a function parameter | allowed | |
C++98 | when omitted, the bound of an array could not be inferred from a previous declaration | inference allowed | |
C++98 | the bound of an array static data member could not be omitted even if an initializer is provided | omission allowed | |
C++11 | auto could not be used as element type | allowed |
for Array declaration |
Array is a data structure that hold finite sequential collection of homogeneous data .
To make it simple let’s break the words.
We can divide arrays in two categories.
Let us understand the significance of arrays through an example.
Suppose, I asked you to write a program to input 1000 students marks from user. Finally print average of their marks .
To solve the problem you will declare 1000 integer variable to input marks. Call I/O functions to input marks in 1000 variables and finally find the average.
Think for a while how tedious will be to code if solved using above approach. Declare 1000 variables, take input in all variables, then find average and finally print its average. The above increases length of code, complexity and degrades performance. If you don’t believe, try to code solution using above approach.
To solve above problem efficiently we use arrays. Arrays are good at handling collection of data (collection of 1000 student marks). Mind that in programming, we will always use some data structure (array in our case) to handle a collection of data efficiently.
Array in memory is stored as a continuous sequence of bytes. Like variables we give name to an array. However unlike variables, arrays are multi-valued they contain multiple values. Hence you cannot access specific array element directly.
For example, you can write sum = 432; to access sum . But, you cannot access specific array element directly by using array variable name. You cannot write marks to access 4 th student marks.
In array, we use an integer value called index to refer at any element of array. Array index starts from 0 and goes till N - 1 (where N is size of the array). In above case array index ranges from 0 to 4 .
To access individual array element we use array variable name with index enclosed within square brackets [ and ] . To access first element of marks array, we use marks[0] . Similarly to access third element we use marks[2] .
Syntax to declare an array.
How to initialize an array.
There are two ways to initialize an array.
We define value of all array elements within a pair of curly braces { and } during its declaration. Values are separated using comma , and must be of same type.
Note: Size of array is optional when declaring and initializing array at once. The C compiler automatically determines array size using number of array elements. Hence, you can write above array initialization as.
You can assign values to an array element dynamically during execution of program. First declare array with a fixed size. Then use the following syntax to assign values to an element dynamically.
Instead of hard-coding marks values, you can ask user to input values to array using scanf() function.
The array index is an integer value, so instead of hard-coding you can wrap array input code inside a loop .
The above code will run 5 times from 0 to 4 . In each iteration it ask user to input an integer and stores it in successive elements of marks array.
Let us write a C program to declare an array capable of storing 10 student marks. Input marks of all 10 students and find their average.
Output –
Practice more array programming exercises to learn more.
IMAGES
VIDEO