MASIGNASUKAv102
6510051498749449419

C++ Arrays - Learn With AVRK

Arrays in C++ language

An array is a set of similar types of elements that have contiguous memory location.

A std::array in C++ is a container that holds fixed-sized arrays. In C++, array index starts at 0. We can store only fixed set of elements in C++ array.

An array of int or float datatypes are arrays but arrays of char datatypes are called strings.

Loading...

Declaration of array

Similar to the variable, arrays are declared before they are being used for the benefit of the compiler. The general form of the declaration an array is,
datatypes array_name[size];

Array initialization

The elements of an array are initialized during declaration like ordinary variables. The general syntax for initialization is,
datatypes array_name[size] = { list_of_values };
                    OR
array_name[index_no.] = single_value;


Advantages of C++ Array

The most common advantage of C++ is that,
  • Code Optimization (less code)
  • Random Access
  • Easy to traverse data
  • Easy to manipulate data
  • Easy to sort data etc.


Disadvantages of C++ Array

  • Fixed size


C++ Array Types

There are 3 types of arrays in C++ programming:
  • Single Dimensional Array
  • Two Dimensional Array
  • Multidimensional Array

Let's look at a simple example of a C++ array,
#include<iostream>
using namespace std;
int main()
{
    int arr[5]={10, 20, 30, 40, 50}; /*creating and initializing array */
    /*traversing array*/
    for (int i = 0; i < 5; i=i+1)
    {
    cout<<arr[i]<<"\n";
    }
    return 0;
}


Output:

10
20
30
40
50

Also check out this : C++ Storage Classes - Learn With AVRK
Learn With AVRK

Learn With AVRK