A char array is a static buffer that allocates x number of bytes on the programs stack (x being the number in char asdf[x]; when declaring a char array), the std::string class differs from char arrays in the sense that it dynamically allocates memory for you on initialization, this way people cannot execute standard stack based buffer overflows into your code.
In C if you do this:
Code:
char static_important_data[] = "aaaaaaaaaa";
char user_defined_data[50];
...
scanf("%s\n",user_defined_data);
printf("%s",static_important_data);
printf("%s",user_defined_data);
This program could go as expected as planned or it could go horribly wrong. If someone enters 55 characters (let's say all B's) on that scanf call, what will that "static_important_data" var be? It'll be aaaaabbbbb (or something not far removed.)
Contra C++ and std::string where I can do this:
Code:
std::string static_important_data = "aaaaaaaaaa";
std::string user_defined_data;
...
std::cin>>user_defined_data;
std::cout<<static_important_data;
std::cout<<user_defined_data;
And I give that cin call
asdkifjaowiejflewkdfjsldkfjasldkjflaskdjflaskdjfal sdkfmasldkfjasdo98fausdflijasdlfkjasdlfusaodfijasd lfkjasdlfkjasodlkfjasldfkajsdlfkajsd
It'll still print out those ten "a"s when you call that cout because std::string automatically adjusts itself to when it's assigned. Another slight advantage is that with std::string you can do integer like comparison because it overloads the == operator.
For more information see here.