In this program:
string s;
char *c="hello";
strncpy((char *) s.data(), c,5);
count<<s;
This is giving BLANK output. Where am I wrong here?
In this program:
string s;
char *c="hello";
strncpy((char *) s.data(), c,5);
count<<s;
This is giving BLANK output. Where am I wrong here?
You started off on the wrong track in wanting to use strncpy to copy to an empty std::string. You could have simply written:
or even:Code:const char *c = "hello"; string s = c;
By the way, notice that I declared c to be a const char*. You made another mistake by writing count instead of cout.Code:string s = "hello";
If you want to copy a substring, then start off with a std::string, after which there are various string functions for substrings.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)