Hi
I am trying to find a way to convert from char * to string.
I have:
char * buf = "test";
and I want to look for a substring in this. I tried using strcpy:
string s;
strcpy (s, buf);
but it keeps telling me: "No instance of function "strcpy" matches the argument list.
Anybody knows a way of doing this?
Thanks
6 replies to this topic
#1
Posted 19 September 2011 - 02:26 PM
|
|
|
#2
Posted 19 September 2011 - 03:50 PM
IDK about that error, but first I must tell you, strcpy is NOT compatible with C++ strings. If you want to convert from char * to string, you would do something like this:
BTW, use the code tags for you code. In the editor, it is the # symbol.
char *but = "test"; string s; s = buf;If you want to use strcpy, however, for copying one C string to another, you must make sure you have the library included:
#include <cstring> //For C++ #include <string.h> //For C
BTW, use the code tags for you code. In the editor, it is the # symbol.
Latinamne loqueris?
#3
Posted 19 September 2011 - 04:57 PM
You can simply use the (overloaded) assignment operator to assign a char * to a std::string. This will copy all the data over automatically.
If you're trying to copy from a std::string to a char *, the best way to do that is with strncpy(), but you have to get the char data from the string first!
If you're trying to copy from a std::string to a char *, the best way to do that is with strncpy(), but you have to get the char data from the string first!
size_t my_str_size_with_null_end = my_str->size() + 1; char *buffer = malloc(my_str_size_with_null_end); strncpy(buffer, my_str->c_str(), my_str_size_with_null_end);
Wow I changed my sig!
#4
Posted 19 September 2011 - 05:01 PM
mebob said:
...
char *but = "test"; string s; s = buf;...
mebob said:
...In the editor, it is the # symbol.
#5
Posted 19 September 2011 - 05:37 PM
@RhetoricalRuvim: Both of them work. I prefer [noparse][code][/noparse] tags myself, along with any other tags I can use. The only real trouble I have with the quick reply window here is that it keeps the formatting of pasted text, which forces me to put it through a text editor first. -_-
Wow I changed my sig!
#6
Posted 19 September 2011 - 06:15 PM
#7
Posted 20 September 2011 - 06:22 AM
A conclusion is where you got tired of thinking.
#define class struct // All is public.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









