I want to know the differences between char * and string. for example in this code:
char *a; string b; Can anyone help me please?
I want to know the differences between char * and string. for example in this code:
char *a; string b; Can anyone help me please?
Assuming you're referring to std::string, string is a standard library class modelling a string.
char* is just a pointer to a single char. In C and C++, various functions exist that will take a pointer to a single char as a parameter and will track along the memory until a 0 memory value is reached (often called the null terminator). In that way it models a string of characters; strlen is an example of a function (from the C standard library) that does this.
If you have a choice, use std::string as you don't have to concern yourself with memory.
string is a class, which data-member of it, holds the text that we assign to string?if you are worrying about functionality, string is a functional char* i.e you need not to worry about space
char*
declaration/initialization: char* str = "Use";
appending: XXX
finding length: strlen(str); //need to include <string.h> or create your own
string
declaration/initialization: string str = "Use";
appending: str += " This!"
finding length: str.length() //all in one header file
char* can also be a pointer to the 0 (first) place of an array of characters. It was used frequently in C, where the use of String is not supported.
Its simple, char *a; declares a pointer 'a' of type char,it will point to a constant string or character arrays. String b; declares b as an object of string type.String here is a class which contains several string manipulation member functions(methods).You can look here for further details:http://www.cplusplus.com/reference/string/string/
One example program describing the string object and its member function is given below:
#include <iostream> #include <string> using namespace std; int main () { string str ("steve jobson"); cout <<"hello the name of steve jobs consists of"<< str.size() << " characters.\n"; return 0; } str is declared as string object and the member function size() is called to get the size of str.