cppreference.com

Array declaration.

(C++20)
(C++20)
(C++11)
(C++20)
(C++17)
(C++11)
(C++11)
General topics
(C++11)
-
-expression
block


/
(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

[ edit ] Syntax

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:

[ edit ] Assignment

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:

[ edit ] Array-to-pointer decay

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:

[ edit ] Multidimensional arrays

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.

[ edit ] Arrays of unknown bound

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.

[ edit ] Array rvalues

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.

[ edit ] Defect reports

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

[ edit ] See also

for Array declaration
  • Recent changes
  • Offline version
  • What links here
  • Related changes
  • Upload file
  • Special pages
  • Printable version
  • Permanent link
  • Page information
  • In other languages
  • This page was last modified on 4 April 2024, at 07:41.
  • Privacy policy
  • About cppreference.com
  • Disclaimers

Powered by MediaWiki

Codeforwin

Arrays in C – Declare, initialize and access

Array is a data structure that hold finite sequential collection of homogeneous data .

To make it simple let’s break the words.

  • Array is a collection – Array is a container that can hold a collection of data.
  • Array is finite – The collection of data in array is always finite, which is determined prior to its use.
  • Array is sequential – Array stores collection of data sequentially in memory.
  • Array contains homogeneous data – The collection of data in array must share a same data type .

We can divide arrays in two categories.

  • One-dimensional array (Or single-dimensional array)
  • Multi-dimensional array

Why we need arrays?

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.

How to use arrays?

Array representation in memory

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] .

How to declare an array?

Syntax to declare an array.

  • data_type is a valid C data type that must be common to all array elements.
  • array_name is name given to array and must be a valid C identifier .
  • SIZE is a constant value that defines array maximum capacity.

Example to declare an array

How to initialize an array.

There are two ways to initialize an array.

  • Static array initialization – Initializes all elements of array during its declaration.
  • Dynamic array initialization – The declared array is initialized some time later during execution of program.

Static initialization of 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.

Example of static array initialization

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.

Dynamic initialization of array

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.

Example to initialize an array 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.

Example program to implement one-dimensional 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 –

Array best practices

  • Arrays are fixed size, hence always be cautious while accessing arrays. Accessing an element that does not exists is undefined. You may get a valid value or the program may crash.For example, consider the below program. int array[5]; // Declare an integer array of size 5 /* Accessing sixth element of array which is undefined. */ array[5] = 50; /* Accessing -1th element of array is undefined */ array[-1] = 10;
  • Always keep in mind that all array element store a value of similar type.

Recommended array example programs

  • Program to read and print array element.
  • Program to find maximum and minimum element in array.
  • Program to insert a new element in array.
  • Program to search an element in array.
  • Program to sort array elements.
Practice more array programming exercises to learn more.

IMAGES

  1. Array in C language

    array assignment syntax

  2. PPT

    array assignment syntax

  3. C++ Array Assignment

    array assignment syntax

  4. C# Arrray: An Introductory Guide for Getting Started

    array assignment syntax

  5. PPT

    array assignment syntax

  6. Arrays

    array assignment syntax

VIDEO

  1. Syntax of ARRAY #coding #javacoding #javalanguage #javafullstackdeveloper

  2. 🔍🔍 Unpacking Objects: Exploring Destructuring in JavaScript! 🔍🔍 #telugu #codewithkg

  3. How to Assign and Re-assign Values to Arrays in C++

  4. Array

  5. Array

  6. how to use the destructuring assignment syntax with an object and an array #javascript #coding