Coming from C, I was always taught that char * and string are very similar to each other. Here is a piece of code that I implemented using string and char * .
The latter gives an error.
prog.cpp: In function 'char* foo()':
prog.cpp:8:15: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings] char *bro="karan";
Why does the latter not work as intended?
The string one:
#include<iostream>
#include<string>
using namespace std;
string foo()
{
string bro="karan";
return bro;
}
int main()
{
cout << foo() << endl;
return 0;
}
The char * one:
#include<iostream>
#include<string>
using namespace std;
char *foo()
{
char *bro="karan";
return bro;
}
int main()
{
cout << foo() << endl;
return 0;
}