Reputation: 11
I mean what is the difference of string in C and C++?
Upvotes: 1
Views: 1669
Reputation: 1258
I fully agree with @pmg answer. But one need to mention some things. In C programmer must be very careful when he works with C-strings because a) every C-string must be ended with zero code character; b) it is very easy to make buffer overrun if buffer size for string is too small. Also in C all work with strings goes through functions. It may be programmers nightmare. In C++ things are much simpler. Firstly, you don't need to care about memory management. String class allocate additional memory when internal buffer becomes small. Secondly, you don't need to care about zero terminating character. You work with container. Thirdly, there are simple methods for working with string class. For example, overloaded operator + for string concatenation. No more awful strcat() calls. Let the work with strings to be simple!
Upvotes: 3
Reputation: 2105
In C there is no such thing/type as "string". It is represented as NULL terminated array of characters like char str[256];
. C++ has string
class in standard library that internally maintains it as array of characters and has many methods and properties to manipulate it.
Upvotes: 4
Reputation: 755
in C++ String objects are a special type of container, specifically designed to operate with sequences of characters.string class defined in string
or in C string is a character sequence terminated with a null character ('\0'), all functions related to strings defined in string.h
Upvotes: 0
Reputation: 108988
C does not define string
: it only has "perfectly ordinary arrays of characters" and pointers to those arrays;
C++ defines it, as a class type, with several properties and methods.
Upvotes: 8