- char*
eg:char* str = "This is GeeksForGeeks";
pros:
- A pointer to the string, to save memory;
- Not need to specify the string size in advance.
cons:
- In the above example, the C language work, but C ++ will prompt warning, therefore, in C ++, to add const modifier. eg: const char * str = "This is GeeksForGeeks";
- C ++ is a constant string array of characters, it can not be modified.
- std::string
std::string str = "This is GeeksForGeeks";
note: std::string class which is an instantiation of the basic_string class template
pros:
- When devoted string, more convenient (searching, replacement, and manipulation functions)
However, there are some cases or more is recommended char * instead of std :: string
- Lower-level access processing, such as communication with the operating system
- Compatibility with older code C
- Save memory
- char[]
eg: char str[] = "This is GeeksForGeeks";
pros:
- Modify the string
cons:
- This is a statically allocated array size, the space occupied by the stack.
- If we want to connect or operate with other strings, we need to open up large arrays, because of the size of the string is fixed. To this end, we can use the C ++ Standard Library cstring or string.h.
reference:https://www.geeksforgeeks.org/char-vs-stdstring-vs-char-c/