s is pointing to "this is a string" but when I initialize it to t,it throws error C2440 so my question is Why compilation fails when I initialize one pointer string to another non pointer string?
#include<iostream>
using namespace std;
int main()
{
char t="5d";
char *s = "this is a string";
cout<<s;
cout<<&s;
*s=t;
cout<<s;
cout<<&s;
return 0;
}
error C2440: 'initializing' : cannot convert from 'const char [3]' to 'char'
*s=t
.std::string s = "this is a string";
std::string
instead ofchar*
. Bjarne Stroustrup says it well: "If you don't want to use arrays and pointers, standard library strings and containers are (better) alternatives in many cases. If you use only low-level facilities, you are almost certainly wasting time and complicating maintenance without performance advantages"