What is the difference between using string type and char[] type to store a name? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is the difference between using string type and char[] type to store a name?

In terms of Performance, Effectiveness? Anything? and what are the good practices?

2nd Aug 2020, 3:12 AM
Prashant Pant
Prashant Pant - avatar
5 Answers
+ 2
In string data type and char[], there is no difference they both same. Char[] is basically an array of characters which make string and string data type is the array of characters so there is no difference. To define a string in c like languages we use char[] because there is no string data type but when c++ and java or other languages comes then they come with a dedicated string data type to create strings easily just using 'string' data type. In terms of performance char[] is faster than string. And also string is an immutable data type. While char[] can be updated.
2nd Aug 2020, 3:40 AM
Maninder $ingh
Maninder $ingh - avatar
+ 2
Using char[] you can manage memory efficiently as it will store specific space for the array of strings whereas using string a specified space will be stored even if it is not filled.
2nd Aug 2020, 6:03 AM
Shrishti Dogra
Shrishti Dogra - avatar
+ 2
Martin Taylor I know I'm not talking about only c++ I posted just general answer.
2nd Aug 2020, 7:45 AM
Maninder $ingh
Maninder $ingh - avatar
+ 2
Maninder $ingh I tend to believe that saying that a C-style string performs better than a C++ string,is nothing short of a myth. In what respect(s) is it better? I think that in most cases,the operations performed on a C++ string are faster than their C-style string counterparts.
3rd Aug 2020, 5:58 AM
Anthony Maina
Anthony Maina - avatar
+ 1
Anthony Maina yes you are right. For more information, if anyone has still doubt and confusion can check this on the StackOverflow. https://stackoverflow.com/questions/1287306/difference-between-string-and-char-types-in-c#:~:text=Strings%20are%20a%20lot%20more,more%20efficient%20for%20the%20computer.&text=Well%2C%20string%20type%20is%20a,a%20character%20string%20for%20you.
3rd Aug 2020, 1:58 PM
Maninder $ingh
Maninder $ingh - avatar