i am learning C++ from a good time now.. but I am still very confused sometimes in the use of array(char) and string.. can anyone please tell me that what exactly all differences between string and char array ? what can i do with array which i can't do with string and vice-versa ? searched google alot but on each place something confusing is written. thanks in advance to you.

Recommended Answers

All 11 Replies

A char array is just that. Some char objects all next to each other in memory, typically with a zero value on the end. It is up to you to allocate it, keep track of how big it is, make sure you don't write or read off the end of it. If you need to expand it, it's a pain.

A string is a proper class object. Buried somewhere deep inside it will be an array of char where the actual letter values are stored. The string object gives to a vast range of functions you can use on that string; http://en.cppreference.com/w/cpp/string/basic_string You can also use the algorithms library on it. It takes care of its own size, expanding as needed so you don't need to take care of that yourself (if you're just using an array of char, it's up to you to manage that sort of thing). Time and experience has shown that it is so very, very easy to screw things up when using an array of char, and the string object goes a long way to eliminating those common sources of error.

The string object is a proper class object and as a rule of thumb, use it everywhere you can in preference to an array of char. The string object even gives you a function enabling you to pretend it is just an array of char, in case you're stuck with a function that wants an array of char instead of a proper string object.

"in case you're stuck with a function that wants an array of char instead of a proper string object." can you explain it again ? didn't get this line . Secondly, your link has no text in that, please check it. thanks to you.

Here is a function:

 someFunction(const char* theInputParameter)
 {
   // some code
 }

It wants char pointer. What a pain. I like to use proper string objects. I'll have to use the c_str() function - http://en.cppreference.com/w/cpp/string/basic_string/c_str - to get the string object to return a pointer to a char so I can pass that pointer to this function.

string someString;

someFunction(someString.c_str())
{
// some code
}

ohh yes!! but can you tell when i declare string s, isn't it itself a pointer ? because it's size(s) will return 4 which is pointer size i think . can you clearify this thing ? and if it is pointer, then why do you still need to use c_str() ? thanks

Give this link a try to see what a string can do.

The string object even gives you a function enabling you to pretend it is just an array of char, in case you're stuck with a function that wants an array of char instead of a proper string object.

Imagine you have this function

int CountVowles(const char *)

Now since this function wants a const char * you cant just pass a string to it. Bummer hun. Well you are in luck. The string class has a member function called c_str() and it will return a const char * to the array that is inside the string object. Now all you have to do in your code to use the function with a string object is

//inside main
string sentence;  // string to hold a sentence
// put a sentence in the string sentence
int numberOfVowles = CountVowles(sentence.c_str());
//...

And now you can still use a function that wants a char array without having to use a char array in your program. As a note this will only work for functions that take a const char *. If the function wants a char * you will either have to copy the string into a char array or you could look for a different function. Chances are you if you have a function that takes a char * that does a standard thing like sort you can probably find what you need in the algorithm header.

commented: nice!! +2

To answer your second question a string is an oject it is not a pointer. it just happened to have a size of 4 when you checked.

but when i add all the characters in the string s, and print afterwards, then again even it's sizeof(s) prints 4. now why this happen ? thanks

sizeof is meaningless for strings. At best you'll get the size of an array, which may or may not correspond to the length of a string contained within that array. At worst you'll get the size of a pointer or the size of the containing object (eg. std::string), neither of which are useful when you want the number of characters in the string.

and if it is pointer, then why do you still need to use c_str() ?

As Deceptikon and Nathan say, a string object is not a pointer. It will contain pointers. It's not a pointer.

If you want to know the length of the string use the size() member function.

and if it is pointer, then why do you still need to use c_str() ?

Array of charaters are considered (in C++) to be C style strings, where they would be just a sequence of characters in the memory.
string on the other hand, is a C++ class that implements the concept of a string. Indeed, at its very core, it uses a C string, but it has multiple functions/operations defined for an easy use.
We use the c_str() function when having a string object at hand, but need its C style array of characters.

In C++ it's better to work with objects/classes rather than memory segments, for a better/safer use of the program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.