Arrays and String Concepts in C++


An array in C++ programming language is a collection of similar data items stored at contiguous memory locations, in short, they are used to keep multiple values in a single variable, rather than declaring different variables for each value.

They can be used to store sequential collection of data types such as int, double, float, char, etc of any particular type. To add to it, an array in C++ can store derived data types such as the structures, pointers, etc.

Each element of the array elements can be accessed randomly using indices of an array. Rather than declaring individual variables as num0, num1, …, num99, we declare one array variable as numbers and use num[0], num[1], and …, num[99] to represent each variables.

All arrays consist of contiguous memory locations. The first element refers the lowest address and the last element refers to the highest address.


datatype arrayName [ arraySize ];

Declaring Array

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

int myNum[3] = {1, 2, 3};

Why do we need arrays?

We can use normal variables when we have a small number of objects, but if we want to store a large number of instances, it becomes difficult to manage them with normal variables. The concept of an array is to represent many instances in one variable.

Advantages of an Array in C++: 

  1. Easy access of elements using the array index.
  2. Reduces the size of the code.
  3. Traversal through the array becomes easy can be done using a single loop.

Disadvantages of an Array in C++: 

  1. Not Dynamic. The size of the array has to be mentioned at the beginning of the program.
  2. Insertion and Deletion of elements can be costly in terms of execution time.
  3. Wastage of memory.


#include <iostream>
using namespace std;

int main() {
    int num[5] = {7, 5, 6, 12, 35};
    cout << "The Numbers are: ";   //  Printing array element
    for (int i = 0; i < 5; ++i) 
        cout << numbers[i] << "  ";

    return 0;

The numbers are: 7  5  6  12  35
Sr.NoConcept & Description
1Multi-dimensional arrays– They are an array of arrays. Eg: 2-D Array.
2Pointer to an array– We can generate a pointer to the first element of an array by specifying the array name, without any index.
3Return array from functions– C++ allows a function to return an array.

Array out-of-bound

For example, If we declare an array of size 100, then the array will contain elements from index 0 to 99.
However, if we try to access the element at index 100 or more than 100, it will result in Undefined Behaviour.

Strings in C++

Strings in simple language, are objects that represent sequences of characters.

C++ provides following two types of string representations −

  • The C-style character string.
  • The string class type introduced with Standard C++.

The C-style character string started within the C programming language and continues to be supported within the C++ programming language. This string is a 1-dimensional array of characters that is terminated by a null character ‘\0’.


char greeting[] = "GeeksGod";

C++ supports a wide range of functions that manipulate null-terminated strings −

Sr.NoFunction & Purpose
1strcpy(s1, s2); Copies string s2 into string s1.
2strcat(s1, s2); Concatenates string s2 onto the end of string s1.
3strlen(s1); Returns the length of string s1.
4strcmp(s1, s2); Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
5strchr(s1, ch); Returns a pointer to the first occurrence of character ch in string s1.
6strstr(s1, s2); Returns a pointer to the first occurrence of string s2 in string s1.
We will be happy to hear your thoughts

Leave a reply

Open chat
Scan the code
Can we help you?