• Home
  • Jobs
  • Courses
  • Certifications
  • Companies
  • Online IDE
  • Login
  • Signup
MYTAT
  • Home
  • Jobs
  • Courses
  • Certifications
  • Companies
  • Online IDE
  • Login
  • Signup
C++
  • C++ Introduction
  • C++ Getting Started
  • C++ Syntax
  • C++ Output (Print Text)
  • C++ Comments
  • C++ Variables
  • C++ Declare Multiple Variables
  • C++ Identifiers
  • C++ User Input
  • C++ Data Types
  • C++ Operators
  • C++ Strings
  • C++ Math
  • C++ Booleans
  • C++ Conditions
  • C++ Switch
  • C++ While Loop
  • C++ For Loop
  • C++ Break And Continue
  • C++ Arrays
  • C++ References
  • C++ Pointers
  • C++ Functions
  • C++ Function Overloading
  • C++ OOP
  • C++ Classes And Objects
  • C++ Class Methods
  • C++ Constructors
  • C++ Access Specifiers
  • C++ Encapsulation
  • C++ Inheritance
  • C++ Multilevel Inheritance
  • C++ Multiple Inheritance
  • C++ Inheritance Access
  • C++ Polymorphism
  • C++ Files
  • C++ Exceptions
  • C++ How To Add Two Numbers
  • Home
  • Courses
  • C++
  • C++ Arrays

C++ Arrays

Previous Next

C++ Arrays

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

To declare an array, define the variable type, specify the name of the array followed by square brackets and specify the number of elements it should store:

string cars[4];

We have now declared a variable that holds an array of four strings. To insert values to it, we can use an array literal - place the values in a comma-separated list, inside curly braces:

string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};

To create an array of three integers, you could write:

int myNum[3] = {10, 20, 30};

Access the Elements of an Array

You access an array element by referring to the index number.

This statement accesses the value of the first element in cars:

Example

string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars[0];
// Outputs Volvo


Try it now


Change an Array Element

To change the value of a specific element, refer to the index number:

Example

cars[0] = "Opel";

Example

string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
cout << cars[0];
// Now outputs Opel instead of Volvo
 

Try it now



Practice Excercise Practice now

Loop Through An Array

You can loop through the array elements with the for loop.

The following example outputs all elements in the cars array:

Example

string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
for(int i = 0; i < 4; i++) {
  cout << cars[i] << "\n";
}

Try it now

The following example outputs the index of each element together with its value:

Example

string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
for(int i = 0; i < 4; i++) {
  cout << i << ": " << cars[i] << "\n";
}

Try it now



Practice Excercise Practice now

C++ Omit Array Size

You don't have to specify the size of the array. But if you don't, it will only be as big as the elements that are inserted into it:

string cars[] = {"Volvo", "BMW", "Ford"}; // size of array is always 3

This is completely fine. However, the problem arise if you want extra space for future elements. Then you have to overwrite the existing values:

string cars[] = {"Volvo", "BMW", "Ford"};
string cars[] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"};

If you specify the size however, the array will reserve the extra space:

string cars[5] = {"Volvo", "BMW", "Ford"}; // size of array is 5, even though it's only three elements inside it

Now you can add a fourth and fifth element without overwriting the others:
cars[3] = "Mazda";
cars[4] = "Tesla";


Try it now


Omit Elements on Declaration

It is also possible to declare an array without specifying the elements on declaration, and add them later:

string cars[5];
cars[0] = "Volvo";
cars[1] = "BMW";
...

Try it now



Practice Excercise Practice now

Previous Next
COMPANY
  • About us
  • Careers
  • Contact Us
  • In Press
  • People
  • Companies List
Products
  • Features
  • Coding Assessments
  • Psychometric Assessment
  • Aptitude Assessments
  • Tech/Functional Assessments
  • Video Assessment
  • Fluency Assessment
  • Campus
 
  • Learning
  • Campus Recruitment
  • Lateral Recruitment
  • Enterprise
  • Education
  • K 12
  • Government
OTHERS
  • Blog
  • Terms of Services
  • Privacy Policy
  • Refund Policy
  • Mart Category
Partner
  • Partner Login
  • Partner Signup

Copyright © RVR Innovations LLP 2025 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP