C++ Array Assignment

Today, we will learn C++ Array Assignment. We can assign multiple values to C++ Arrays. There are many ways you can initialize a C++ array. You can create different datatypes of arrays in C++ e.g. string, int, etc are the two most common types of C++ arrays that are commonly used. Today, we will see how to initialize and assign values to C++ arrays. Let’s start.

Declaring Empty C++ Array

We can declare an empty array in C++ using square brackets and specifying the number of items.

string city[5];
int id[6];

Declaring and C++ Array Assignment

We can declare and initialize a C++ Array at the same time. To initialize a C++ Array while declaring, we can use curly brackets and then values inside curly brackets.

string city[5] = {"Agra", "Delhi", "Noida", "Gurgaon", "Banglore"};

Accessing the elements of the C++ Array

You can use square brackets to access the elements of the C++ Array. Inside square brackets, use the index number of the element you want to access. Indexing in C++ starts from 0.

cout << city[0];
// Outputs Agra

Looping a C++ Array

#include <iostream>
#include <string>
using namespace std;
int main() {
string city[5] = {"Agra", "Delhi", "Noida", "Gurgaon", "Banglore"};
for(int i = 0; i < 5; i++){
    cout << city[i] << "\n";
}
  return 0;
}

Output:

C++ Array Assignment for loop

Also Read:

Share:

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you