string a="cplus";
char* b=a; // THIS STEP IS SHOWING ERROR
EVERY STRING AS WE KNOW IS A CONSTANT POINTER. BUT WHEN WE INITIALIZE POINTER TO A STRING THAT IS DEFINED BY KEYWORD STRING AS SHOWN ABOVE, COMPILER IS DISPLAYING
ERROR:CANNOT CONVERT STD::STRING TO POINTER
ARE THE STRINGS NOT POINTERS IF THEY ARE DEFINED AS IN STEP ONE? IF SO PLEASE TELL THE REASON?
std::string is not a pointer, but a typedef of the basic_string class. You can't assign a non-pointer to a pointer. Consider the following code as a possible solution:
1 2 3 4 5 6 7 8
string a = "cplus";
char* b = newchar[a.length() + 1]; //create a new character array
b = strcpy(b, a.c_str()); //copy the contents of a into b
a = "hello world!"; //simply showing that if a changes, b will not change
std::cout << a << '\n' << b << std::endl; //output the data of each variable
delete[] b; //delete the dynamically allocated array
b = 0; //set the pointer to 0