Arrays in C/C++



Arrays

An array is a collection of similar data items stored at contiguous memory locations and elements can be accessed randomly using indices of an array. They can be used to store collection of primitive data types such as int, float, double, char, etc., of any particular type. To add to it, an array in C/C++ can store derived data types such as the structures, pointers etc.


Given below is the picture representation of an array.



Syntax:


Single -Dimensional Array: The ArraySize must be integer constant greater than Zero and type can be any valid C Data Type.

type ArrayName [ ArraySize ];
double balance [10];


Example:

#include <iostream>
#include <string>
using namespace std;

int main() {
  string cars[4] = {"Volvo", "BMW", "Ford", "Ferrari"};
  cout <<"Output of Cars[2] array is " <<" " << cars[2];
  return 0;
}

Output: