I dont understand the difference between C strings and C++ strings

Can somebody give me a good explanation on the difference? How are each strings stored different in memory? I know that C strings are stored as an array of chars ending with a null terminator but how is a c++ string different?
bump!
Can somebody give me a good explanation on the difference?


C doesn't have a special type for strings. Instead, it just treats arrays of chars slightly differently, and provides some string manipulation functions that work with char arrays.

For those functions to work properly, they assume the string has a null terminator.


C++ is basically the same, but instead of making you deal with the raw data manually, it wraps it all up inside of a class. This allows you to resize strings to any length effortlessly without having to worry about memory allocation issues. It also is able to optimize some things that the C functions are unable to, and just generally be overall easier to use.

How are each strings stored different in memory?


They're not really. The string data itself essentially is just a char array, both in C and in C++.

The difference with C++'s string class is that you simply don't access the char array directly like you do with C strings. Instead you work with a 'string object'.



So really the difference is in how you use them, rather than how they're stored. You can do the same things with both, though:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
////////////////////////////////////////
////////////////////////////////////////
//  do some stuff with a C string
char foo[100];  // an array -- note the size means we cannot exceed a string length of 99 chars

// assign new string data
strcpy( foo, "Some new string data." );

// Append some data to the end of that string
strcat( foo, " This will be added to the end." );

// get the length of our string
size_t len = strlen( foo );

// cut off the last 4 characters of the string (by moving the terminator up)
foo[len - 4] = 0;

// combine 2 strings together into a new string
//  by copying, then concatenating
char bar[100];
strcpy( bar, foo );
strcat( bar, foo );


////////////////////////////////////////
////////////////////////////////////////
//  do all the same stuff with C++ strings
std::string foo;  // <- no need to specify a length.  Strings can be any size.
                  //  The string class will resize to make its data bigger as necessary
     
// assign new string data
foo = "Some new string data.";

// append some data
foo += " This will be added to the end.";

// get the length
size_t len = foo.length();

// cut off the last 4 characters of the string (by using substr)
foo = foo.substr(0, len-4);

// combine 2 strings together into a new string
std::string bar = foo + foo;



So as you can see, the interface for C++ strings is a little easier, and it does some things a little better, which is why if you're using C++, you probably should favor std::string over char arrays. Though there are some rare situations where char arrays are better.
Topic archived. No new replies allowed.