C++ Array

In C++, an array is a collection of elements of the same data type, stored in contiguous memory locations. The size of the array is fixed once it’s declared.

Syntax:


type arrayName[arraySize];
  1. type: The data type of the array elements (e.g., int, float, char).
  2. arrayName: The name you want to give to the array.
  3. arraySize: The number of elements the array can hold (a constant or fixed number).

Array Declaration and Initialization

Declaring and initialize integer array.


int arr[4]; // Array of 4 integers
arr[0] = 1; // Assign 1 to the first element
arr[1] = 2; // Assign 2 to the second element
arr[2] = 3; // Assign 3 to the third element
arr[3] = 4; // Assign 4 to the fourth element

Declaring and Initialize integer array with values.


int arr[4] = {1,2,3,4}; // Array of 4 integers

Declaring and Initialize Float array with values.


float arr[4] = {1.1, 2.2, 3.3, 4.4}; // Array of 4 floats

Declaring and initializing a character array


float arr[4] = {'a', 'b', 'c', 'd', '\0'}; // Array of characters (null-terminated string)

Accessing Array Elements

Array elements are accessed using an index, where the index starts from 0.

Example 1: access integer values


#include <iostream>
using namespace std;

int main() {
   int arr[4] = {1,2,3,4}; // Array of 4 integers
    cout << arr[0] << "\n";  // Outputs 1
    cout << arr[1] << "\n";  // Outputs 2
    cout << arr[2] << "\n";  // Outputs 3
    cout << arr[3] << "\n";  // Outputs 4
    return 0;
}

Output:

1
2
3
4

Example 2: access string values


#include <iostream>
#include <string> // For using the string type
using namespace std;

int main() {
   string empNames[4] = {"John","Tom","Mark","Smith"}; // Array of 4 names
    cout << empNames[0] << "\n";  // Outputs John
    cout << empNames[1] << "\n";  // Outputs Tom
    cout << empNames[2] << "\n";  // Outputs Mark
    cout << empNames[3] << "\n";  // Outputs Smith
    return 0;
}

Output:

John
Tom
Mark
Smith

Update the Array Element values

You can update array elements value through index.

Example 1: Suppose, You want to change value from 3 to 30


#include <iostream>
using namespace std;

int main() {
   int arr[4] = {1,2,3,4}; // Array of 4 integers
    arr[2] = 30;
    cout << arr[0] << "\n";  // Outputs 1
    cout << arr[1] << "\n";  // Outputs 2
    cout << arr[2] << "\n";  // Outputs 30
    cout << arr[3] << "\n";  // Outputs 4
    return 0;
}

Explanation:

  1. Firstly check 3 value is on which index.
  2. found 3 value is on 2nd index.
  3. after that, change value from 3 to 30 through arr[2] = 30

Example 2: update the string value from Tom to Rom


#include <iostream>
#include <string> // For using the string type
using namespace std;

int main() {
   string empNames[4] = {"John","Tom","Mark","Smith"}; // Array of 4 names
    empNames[1] = "Rom";
    cout << empNames[0] << "\n";  // Outputs John
    cout << empNames[1] << "\n";  // Outputs Tom
    cout << empNames[2] << "\n";  // Outputs Mark
    cout << empNames[3] << "\n";  // Outputs Smith
    return 0;
}

Explanation:

  1. Firstly check Tom value is on which index.
  2. found Tom value is on first index.
  3. after that, change value from Tom to Rom through empNames[1] = Rom