String over char[] ?

Ive tryd to find the answer to this, but cant. What is better to use? A string variable or a char[] variable. (eg. string Name = "jack" or char Name[5] = ("jack") ). I plan on useing it as a class attribute, that will be accesable through accesor functions within the class.
My recommendation: Use string.

- You can create string Name[x]; for multiple string, and still easier to read rather than char[x][5]; .
- You won't be bothered with null character termination.
- Many convenient operation available on string. See and compare:
http://www.cplusplus.com/reference/string/string/
http://www.cplusplus.com/reference/clibrary/cstring/
- String itself is a class. Except you are dealing with multiple string, you can directly use it.
Agreed.

Remember, a std::string is a container class, which maintains a char[] internally.

The advantage is now you can use all the dynamic features of a container class, but you still have access to the char[].

Depending on the STL implementation, the char[] is marginally faster than a std::string, but stick with the string unless your profiler tells you that you absolutely have to trim time spent on essential string operations. (This should be unlikely.)

Hope this helps.
Thanks alot!! You guys answerd my question perfectly!! Great site!
Topic archived. No new replies allowed.