What are the Differences Between C++ Char*, std:string, and Char[]?
Introduction
The computer can understand only the language of binary. When a char data type is declared in C or C++. It is stored as an integer value, known as ASCII.
Char* | std::string | Char[] |
---|---|---|
The char* in C++ is a pointer used to point to the first character of the character array. | The std::string is a standard library that includes support for strings in C++. | The char[] is a character array that stores all the characters in the string. |
If we declare a char datatype with a, The ASCII value of a, i.e., 97. It is important to note that the ASCII value for a differs from that of A.
Below is the table with ASCII values of Alphabets from A to Z and a to z.
Letter | ASCII code | Letter | ASCII code |
---|---|---|---|
A | 65 | a | 97 |
B | 66 | b | 98 |
C | 67 | c | 99 |
D | 68 | d | 100 |
E | 69 | e | 101 |
F | 70 | f | 102 |
G | 71 | g | 103 |
H | 72 | h | 104 |
I | 73 | i | 105 |
J | 74 | j | 106 |
K | 75 | k | 107 |
L | 76 | l | 108 |
M | 77 | m | 109 |
N | 78 | n | 110 |
O | 79 | o | 111 |
P | 80 | p | 112 |
Q | 81 | q | 113 |
R | 82 | r | 114 |
S | 83 | s | 115 |
T | 84 | t | 116 |
U | 85 | u | 117 |
V | 86 | v | 118 |
W | 87 | w | 119 |
X | 88 | x | 120 |
Y | 89 | y | 121 |
Z | 90 | z | 122 |
Creating a String Using C++ Char*
Introduction
The char* in cpp is a pointer used to point to the first character of the character array. The char* is usually used to iterate through a character array.
Syntax
The syntax of the char* in C++ is as follows:
Example code
Here is an example of char* in cpp.
Output
The output for the above code is as follows.
We can see that the compiler shows a warning. Let us discuss the warning in the pros and cons section.
Pros and Cons
Pros
- The char* is memory efficient as only one pointer is required to refer to the string.
- The size of the string does not require declaration beforehand.
- The size of the array can be allocated dynamically.
Cons
- Char* works fine in C programming. But when we use char* in Cpp, we get a warning similar to what we saw in the example. This is because it forbids converting a string constant to char*. To avoid this warning, we must use the word const.
Example
Output
Creating a String Using std::string
Introduction
In Cpp, we can represent a sequence of characters as an object of the class. This can be done using the std::string. The cpp string library provides many built-in functions such as append(), compare(), substr() etc., making std::string easy to work with strings.
Syntax
The syntax of std: string in C++ is as follows:
Example code
Here is an example of std::string in cpp.
Output
The output for the above code is as follows.
Pros and Cons
Pros
- The std::string provided flexibility, better searching, and manipulation of strings with its built-in function.
Cons
- std::string has more overhead, consuming a lot of memory compared to other ways.
- std::string is not compatible with old C code.
Creating a String Using Using Char[]
Introduction
The char[] is a character array that stores all the characters in the string. The character array ends with a Null character /0. The size of the array is declared inside the []. A character array can be initialized even without the size of the array.
Syntax
The syntax of Char[] in C++ is as follows:
Example code
Here is an example of char[] in cpp.
Output
The output for the above code is as follows.
Pros and Cons
Pros
- Character Arrays are faster to implement as compared to Strings.
- Specific characters in a string can be changed easily by using its index number.
Cons
- The array size, after the declaration, cannot be changed.
- Character arrays do not have inbuilt functions for string manipulation.
When to Prefer Char* over std ?
In the previous example, We saw that the compiler would show a warning when we used char*. To avoid the warning, we use the const keyword. The const keyword will make the string constant, thus leaving no room for string manipulation.
char* can be used when we declare a constant string. if we are dealing with string manipulation, using std::string is suggested.
When Should You Use Char and When Not?
Using char is preferred as there is no string keyword in Cpp. Further string library in cpp gives a wide scope of built-in functions for string manipulation.
char is preferred when dealing with constant values or a single character.
The usage of char is not preferred when dealing with huge data, and manipulation of the string is involved.
What are C++ Escape Sequences?
A few tokens in Cpp have special meanings such as '(Single quote), "(double quote), \(Backslash), etc. We can not use a few of the tokens directly. Therefore we make use of backslash characters. Given below are a few of the backslash characters in Cpp.
Column 1 | Column 2 |
---|---|
Newline | \n |
Backslash | \ |
Horizontal tab | \t |
Question mark | ? or ? |
Vertical tab | \v |
Single quote | ' |
Backspace | \b |
Double quote | " |
Carriage return | \r |
The null character | \0 |
Form feed | \f |
Octal | \ooo |
Alert (bell) | \a |
Hexadecimal | \xhhh |
Related Articles
Conclusion
- char* is a pointer to a char.
- std::string is a string (class) from the standard (template) library.
- char[] indicates a character array.
- Backslash characters are used when we use a few tokens with special meanings.