![]() |
In C++, we have character array and std::string class both of which are used to store the sequence of characters. The character arrays are a part of C-style programming on the other hand std::string is a part of the C++ standard library. In this article, we will discuss what are some major differences between the character array and std::string objects in C++. Array of CharactersThe array of characters or char array in C++ is used to store a sequence of characters in contiguous memory locations. It is terminated by a null character ( Syntaxchar charArrayName[size];
ExampleC++
Output
Hello, Geek! std::string in C++The std::string is a class in C++ which is a part of STL (standard template library) and used to store sequence of characters. It is vastly used as it handles memory allocation and resizing automatically. Syntaxstring stringName = " "
ExampleC++
Output
Hello, Geek! Hello, Geek! How are you? Length of the string: 25 Difference between Char Array and String in c++The below table illustrates the major differences between char array and string in C++.
ConclusionIn conclusion char arrays are mainly used because it offers simplicity and compatibility with C, making them suitable for certain scenarios. However, string provides a more robust features and is very easy to handle as it is dynamic and can easily handle memory allocation and resizing automatically. |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |