Character Arrays and Strings
Learn how to store alphabets and words using character arrays and strings in C++.
We'll cover the following...
In C++, when we want to work with text, we can use either a string object or a character array. For now, think of these as different ways to store and handle the text. In this lesson, we’ll explore both concepts and learn how they work.
What is a character array?
A character array is a sequence of individual characters stored in consecutive memory locations. Think of it like a train with multiple compartments, where each compartment can hold one character. We can declare a character array using the char
keyword followed by parenthesis []
with the number of characters.
For example, a character array named Educative
containing 8
characters can be declared as follows:
char Educative[8];
In memory, it can be represented as:
Key points
Here are some of the key points about character arrays:
A character array has a fixed size that must be declared when created.
The memory of a character array is allocated during compilation.
Each index in a character array can only hold a single character. ...