Character arrays let you store and work with text in C++. They are simple ways to hold words or sentences as a list of letters.
Character arrays in C++
char arrayName[size]; // Example: char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
The size must be enough to hold all characters plus the special '\0' character that marks the end of the text.
The '\0' character is called the null terminator and tells C++ where the text ends.
char word[5] = {'H', 'e', 'l', 'l', 'o'}; // No null terminator, not a proper string
char word[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; // Proper string with null terminator
char empty[1] = {'\0'}; // Empty string with only null terminator
char greeting[] = "Hi!"; // Size is automatically set to 4 (3 letters + '\0')
This program creates a character array holding "Hello". It prints the word, changes the first letter to 'Y', then prints the new word "Yello". Finally, it prints each letter separately.
#include <iostream> int main() { // Create a character array to hold the word "Hello" char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; // Print the greeting std::cout << "Greeting: " << greeting << std::endl; // Change the first letter greeting[0] = 'Y'; // Print the changed greeting std::cout << "Changed Greeting: " << greeting << std::endl; // Show each character one by one std::cout << "Characters: "; for (int i = 0; greeting[i] != '\0'; i++) { std::cout << greeting[i] << ' '; } std::cout << std::endl; return 0; }
Time complexity to access or change a character is O(1) because you use the index directly.
Space complexity is fixed by the array size you declare.
Common mistake: forgetting the null terminator '\0' causes unexpected behavior when printing or using string functions.
Use character arrays when you want simple, fixed-size text storage without extra features of std::string.
Character arrays store text as a list of characters ending with a null terminator '\0'.
Always leave space for the null terminator to mark the end of the text.
You can change characters by accessing array positions directly.
Practice
Solution
Step 1: Understand the role of '\0' in character arrays
The null terminator '\0' is a special character used to mark the end of a string in C++ character arrays.Step 2: Identify its effect on string handling
Functions that work with strings stop reading characters when they encounter '\0', so it tells where the string ends.Final Answer:
It marks the end of the string stored in the array. -> Option AQuick Check:
Null terminator = end of string [OK]
- Thinking '\0' initializes characters to zero
- Confusing '\0' with increasing array size
- Assuming '\0' changes character case
Solution
Step 1: Count characters and null terminator
The word "code" has 4 letters plus 1 null terminator '\0', so the array size must be 5.Step 2: Check each option's array size and initialization
char word[5] = {'c', 'o', 'd', 'e', '\0'}; correctly declares a char array of size 5 and includes the null terminator explicitly.Final Answer:
char word[5] = {'c', 'o', 'd', 'e', '\0'}; -> Option DQuick Check:
Array size = letters + 1 for '\0' [OK]
- Forgetting to add space for the null terminator
- Using incorrect array size
- Not including '\0' explicitly when needed
char arr[6] = "hello"; arr[0] = 'H'; std::cout << arr << std::endl;
Solution
Step 1: Understand the initial array content
The array is initialized with "hello" which includes the null terminator automatically, so arr = {'h','e','l','l','o','\0'}.Step 2: Modify the first character and print
Changing arr[0] to 'H' updates the first letter. Printing arr outputs the string until '\0', so output is "Hello".Final Answer:
Hello -> Option BQuick Check:
Changing arr[0] changes first letter [OK]
- Expecting output to be lowercase 'hello'
- Thinking only one character prints
- Assuming compilation error due to modification
char name[4] = "John"; std::cout << name << std::endl;
Solution
Step 1: Check array size vs string length
The string "John" has 4 characters plus a null terminator, so it needs an array of size 5.Step 2: Understand consequences of small array
Declaring name[4] leaves no space for '\0', causing undefined behavior when printing.Final Answer:
Array size is too small to hold the string and null terminator. -> Option CQuick Check:
Array size must include space for '\0' [OK]
- Ignoring null terminator space
- Thinking printing char arrays is invalid
- Confusing syntax errors with size errors
Solution
Step 1: Declare array with correct size for "Hi!"
The phrase "Hi!" has 3 characters plus 1 for '\0', so array size must be 4.Step 2: Replace '!' with '?' at correct index
The '!' is at index 2, so phrase[2] = '?' correctly replaces it.Step 3: Print the modified phrase
Printing phrase outputs "Hi?" as expected.Final Answer:
char phrase[4] = "Hi!"; phrase[2] = '?'; std::cout << phrase << std::endl; -> Option AQuick Check:
Array size = 4; replace index 2 [OK]
- Using array size too small to hold string and '\0'
- Replacing wrong index character
- Confusing initialization with replacement
Ready to prove it?
Pass the test and this pattern moves unknown
