CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Cyber Safety and Ethics · 7 min · free

Arrays and Strings in C++

7 min4 KwickClipsFull text belowFree
Next lesson →Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

A C++ array stores same-type values under one name with indexes 0 to size - 1; std::string is a string that grows by itself.

Follows the syllabus of: NIOS Senior Secondary Computer Science (330)

On screen in this lesson

What is an array?

A group of values of the same type
Stored side by side under one name
Each value is reached by its index
Index starts at 0 and ends at size - 1

Memory picture of runs

IndexValueNote
runs[0]45First element
runs[1]12
runs[2]78Highest score
runs[3]30
runs[4]64Last element
runs[5]?Out of bounds!

C string vs std::string

Pointchar arraystd::string
SizeFixedGrows itself
Ends with'\0' nullManaged for you
Copystrcpy(a, b)a = b
Joinstrcat(a, b)a + b
Lengthstrlen(a)a.length()
Comparestrcmp(a, b)a == b

Quick recap

Array: same-type values under one name; index 0 to size - 1
2D array: rows and columns, visited with nested loops
char arrays end with '\0'; std::string grows by itself
Use getline to read a line with spaces
Arrays pass by address, so functions can change them

Quick answers

Why does cin >> name store only Riya from Riya Shah?

cin stops at the first space. Use getline.

Is an array copied when passed to a function?

No. Its address is passed, so changes reach the original.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. Your class has forty students. Will you create forty separate variables for their marks? No! Today we learn arrays, which store many values under one name. We will cover one and two dimensional arrays, two kinds of strings, array programs, and passing arrays to functions.

First, what is an array? An array is a group of values that all have the same data type. The values sit side by side in memory, under one name. We reach each value using its position number, called the index or subscript. The index starts at zero, so the last index is always one less than the size.

This array called runs holds the scores of five batters. The number in square brackets is the size, and the values go inside curly braces. Runs of zero is the first score, forty five. Runs of four is the fifth and last score, sixty four. Pause and predict. What happens with runs of five? There is no such element, and C plus plus does not check, so you get a garbage value or a crash.

Picture the array as a row of boxes. Index zero holds forty five, the first element. Index one holds twelve. Index two holds seventy eight, the highest score. Index three holds thirty. Index four holds sixty four, the last element. Index five is outside the array, which is called out of bounds, and it is a serious bug.

Loops and arrays work together. Here the array a holds the five runs. The loop counter goes from zero to four, one index at a time. Each score is added to sum. At the same time, if a score is bigger than big, it becomes the new big. Big starts as the first element, not zero, so it works even for negative numbers. The total is two hundred twenty nine and the highest is seventy eight.

Linear search checks each element one by one until it finds the value. Here we look for roll number twenty one. Pos starts at minus one, which means not found. The loop compares each element with twenty one, using double equals. It is found at index two, which is the third position.

A two dimensional array is a table with rows and columns, like a marks register. The first size is the number of rows, and the second is the number of columns. So marks has two rows of three marks each, one row per student. Marks of one, two means row one, column two. Both start from zero, so it is the second student's third mark, eighty five.

To visit every cell, we use a loop inside a loop. The outer loop picks a row, and the inner loop moves across its columns. For each row, t, the total, starts at zero and adds three marks. The first student's total is two hundred forty. The second student's total is two hundred twenty.

Now strings. A C style string is an array of characters that ends with a special null character, written as backslash zero. Riya has four letters, so she needs five boxes, including the null character. The function string length, from the header C string, counts the letters, so it prints four. You cannot copy these strings with an equals sign, so you use functions like string copy.

Modern C plus plus gives the string class, from the string header. A string grows by itself, so there is no fixed size to worry about. You join strings with plus and copy them with a single equals sign. City plus space City gives Surat City. Its length is ten, because the space also counts.

Let us compare the two kinds of strings. A char array has a fixed size, while a string object grows by itself. A char array must end with the null character, but a string object manages that for you. To copy, a char array needs string copy, while a string just uses equals. To join, use string cat, or simply plus. For length, use string length, or the length function. To compare, use string compare, or simply double equals.

Here is a common trap. See in with the extraction operator stops reading at the first space. So if you type Riya Shah, only Riya is stored. The get line function, from the string header, reads the whole line, including spaces. If you type Riya Shah, this program prints Hello, Riya Shah.

To pass an array to a function, write the parameter with empty square brackets. In the call, write only the array name, with no brackets. The function cannot know the array size, so we also pass the size, here four. Total adds the four marks and returns three hundred.

Here is an important exam point. An array is not copied when passed. The function receives the address of the first element. So any change inside the function changes the original array. Bonus adds five to every mark, and marks of zero in main becomes sixty five. To stop accidental changes, write const before the parameter.

Let us revise what we learned today. An array stores values of the same type under one name, with indexes from zero to size minus one. A two dimensional array has rows and columns, and we visit it with nested loops. A char array string ends with the null character, while a string object grows by itself. Use get line to read a line with spaces. And arrays are passed by address, so a function can change them. Try finding the lowest score in the runs array yourself.

Courses that teach this

CourseUnit
NIOS Senior Secondary Computer Science (330)Module 3: Programming in C++
Programming All levels C++Arrays, Strings and Pointers

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →