What are the Differences Between C++ Char*, std:string, and Char[]?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

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::stringChar[]
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.

LetterASCII codeLetterASCII code
A65a97
B66b98
C67c99
D68d100
E69e101
F70f102
G71g103
H72h104
I73i105
J74j106
K75k107
L76l108
M77m109
N78n110
O79o111
P80p112
Q81q113
R82r114
S83s115
T84t116
U85u117
V86v118
W87w119
X88x120
Y89y121
Z90z122

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 1Column 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

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.