C++ Arrays

An array is a data structure used for storing a collection of values that all have the same data type. An array can be imagined as a series of data storage locations. Each storage location is called an element of the array.

In C++, array index starts from 0. We can store only fixed set of elements in C++ array.

Java C array 1

There are 2 types of arrays in C++ programming:

  1. Single Dimensional Array
  2. Multidimensional Array

All the below sections are about single dimensional array.

Array Declaration and Allocation

An array can be declared as a normal variable declaration, but in addition append a set of square brackets following the array’s name. The brackets contain the number of elements in the array. The default values for these elements are the same as for variables. The elements in global arrays are initialized to their default values and elements in local arrays remain uninitialized.

Example
snippet
int myArray[3]; // integer array with 3 elements

Array Assignment

To assign values to the elements you can reference them one at a time by placing the element’s index inside the square brackets, starting with zero.

Example
snippet
myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;

Array Initialization

You can initialize a simple array of built-in types, such as integers and characters by assigning values at the same time as the array is declared by enclosing them in curly brackets. The specified array length may optionally be left out to let the array size be decided by the number of values assigned.

Example
snippet
int myArray[3] = { 1, 2, 3 };
int myArray[] = { 1, 2, 3 };

Once the array elements are initialized they can be accessed by referencing the index of the element you want.

snippet
std::cout << myArray[0]; // 1

Dynamic Arrays

The arrays in the previous sections are made up of static (non-dynamic) memory, their size must be determined before execution. Therefore, the size needs to be a constant value. To create an array with a size that is not known until run-time you need to use dynamic memory, which is allocated with the new keyword and must be assigned to a pointer or reference.

snippet
int* p = new int[3]; // dynamically allocated array

An array in C++ behaves as a constant pointer to the first element in the array. The referencing of array elements can therefore be made just as well with pointer arithmetic. By incrementing the pointer by one you move to the next element in the array, because changes to a pointer’s address are implicitly multiplied by the size of the pointer’s data type.

Example
snippet
*(p+1) = 10; // p[1] = 10;

Array Size

We can count the number of elements in an array using the built-in C++ function sizeof() .

Example #1
snippet
int post[] = { 10, 20, 30, 40, 50 };

const int size = sizeof(post) / sizeof(post[0]);
//(or)
int length =  sizeof(post)  / sizeof(int); // 5

This method cannot be used for dynamically allocated arrays. The only way to determine the size of such an array is through the variable used in its allocation.

Example #2
snippet
int size = 3;
int* p = new int[size]; // dynamically allocated array

When you are done using a dynamic array you must remember to delete it. This is done using the delete keyword with an appended set of square brackets.

snippet
delete[] p; // release allocated array

Traversing Array

Example #1

Let's see a simple example of C++ array, where we are going to create, initialize and traverse array.

snippet
#include <iostream>
using namespace std;
int main()
{
 int arr[5]={10, 0, 20, 0, 30};  //creating and initializing array  
        //traversing array  
        for (int i = 0; i < 5; i++)  
        {  
            cout<<arr[i]<<"\n";  
        }  
}
Output
10 0 20 0 30
Example #2

C++ Array Example: Traversal using foreach loop

We can also traverse the array elements using foreach loop. It returns array element one by one.

snippet
#include <iostream>
using namespace std;
int main()
{
 int arr[5]={10, 0, 20, 0, 30}; //creating and initializing array  
        //traversing array  
       for (int i: arr)   
        {  
            cout<<i<<"\n";  
        }  
}
Output
10 20 30 40 50
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +