CHAPTER 10
Beginner
Arrays and Strings in C++
Updated: May 17, 2026
5 min read
# CHAPTER 10
Arrays and Strings
1. Introduction
If you need to store the scores of 50 students, creating 50 separate variables (score1, score2, etc.) is a nightmare. Arrays allow you to store multiple values of the same data type in a single, contiguous block of memory. Strings are essentially arrays of characters used to store text.
2. Learning Objectives
By the end of this chapter, you will be able to:- Declare and initialize arrays.
- Access and modify array elements using loops.
- Create multi-dimensional arrays (matrices).
-
Differentiate between C-style strings and C++
std::string.
- Use built-in string methods.
3. Arrays in C++
An array is a collection of elements of the same type placed in contiguous memory locations.Syntax: dataType arrayName[arraySize];
cpp
4. Looping Through Arrays
Loops and arrays are a perfect match.
cpp
5. Multidimensional Arrays
Arrays can have multiple dimensions. A 2D array is like a grid or a spreadsheet (Rows and Columns).
cpp
6. C-Style Strings (Character Arrays)
In the C language, strings were just arrays of characters ending with a special null character\0. While you can still use them in C++, they are hard to work with.
cpp
7. Modern C++ Strings (std::string)
C++ provides a much better way to handle text: the string class (requires #include <string>).
cpp
8. Mini Project: Student Marks Management
cpp
9. Memory-Level Explanation
Arrays are stored in contiguous memory. If anint is 4 bytes, and you have int arr[3], the OS allocates 12 consecutive bytes.
text
The variable name arr actually holds the memory address of the first element (0x100).
10. Common Mistakes
-
Out of Bounds Error: Accessing
scores[5]in a 5-element array. Valid indices are 0 to 4. C++ does NOT check array bounds; it will simply read random garbage memory or crash (Segmentation Fault).
-
String Concatenation Bug:
string x = "Hello" + "World";is illegal because you can't add two raw string literals. At least one must be astd::stringvariable.
11. Exercises
- 1. Create an array of 10 integers. Use a loop to find and print the largest number in the array.
-
2.
Create a
stringvariable. Use a loop to print the string backwards.
- 3. Write a program using a 2D array to represent a 3x3 Tic-Tac-Toe board and print it to the screen.
12. MCQ Quiz with Answers
Question 1
Array indices in C++ start at:
Question 2
What happens if you access an array out of its bounds (e.g., index 10 in a 5-element array)?
Question 3
Which library is required for modern C++ strings?
Question 4
What is the null terminator used in C-style character arrays?
Question 5
How do you find the length of a std::string str?
Question 6
What does int matrix[3][4] create?
string a primitive data type in C++ (like int or char)?
a) Yes b) No, it is a class from the Standard Library
Answer: b) No, it is a class from the Standard Library
Question 8
In the memory layout of int arr[2], if arr[0] is at address 1000, where is arr[1] (assuming 4-byte ints)?
Question 9
Which loop is best for iterating over all elements in an array without using indices?
13. Interview Questions
- Q: Explain why C++ arrays do not have bounds checking, and what the consequences are.
-
Q: Differentiate between a C-string (
char[]) and a C++std::string. Which is preferred and why?
- Q: How do you pass an array to a function? Does it pass by value or by reference? (Answer: By reference (pointer)).
14. Summary
Arrays group variables of the same type into a single, contiguous block of memory, accessed via zero-based indexing. Modern C++ relies on thestd::string class for robust and safe text manipulation, replacing the older, error-prone C-style character arrays.