how can i substr char * compiler gives error for type
tahnx for ur help
Printable View
how can i substr char * compiler gives error for type
tahnx for ur help
convert char * into a string object first.
Code:string a;
char *l="string";
a=l;
now i have to convert it to str->char*
i used `=` it gives error
thanx for ur help
How about posting
- Your OS
- Your compiler
- Your code.
"i used `=` it gives error" is the most useless thing you can say, be specific!
Do you mean converting from a C++ string to a null-terminated character array?
do this:Code:std::string s = "Hello";
char* cs = s.c_str();
OS : Linux ZenWalk
Compliler : g++
c++
error:Code:void UdpServer::InsertIn2DB(char * err_msg){
Socket2DB<<err_msg;
// substr yapilacak burda
string temp;
temp=err_msg;
temp=temp.substr(25);
err_msg = temp.c_str();
}
udpServerClass.cpp: In member function `void UdpServer::InsertIn2DB(char*)':
udpServerClass.cpp:140: error: invalid conversion from `const char*' to `char*'
make: *** [udpServerClass.o] Error 1
thanx for ur help
if you want to modify the passed string use strcpy. assignement doesn't have any effect because the pointer err_msg is a copy
KurtCode:void UdpServer::InsertIn2DB(char * err_msg){
Socket2DB<<err_msg;
// substr yapilacak burda
string temp;
temp=err_msg;
temp=temp.substr(25);
strcpy(err_msg , temp.c_str());
}
strcpy() is in <cstring>.
char[] strings (c strings) don't have overloaded operators like +, =, and == like C++ strings do. Unless you have a reason not to, you should use the string class in <string>.
since this is obvious c++ program why not just make the parameter a std::string object instead of char*. That way you can remove the strcpy(), get rid of temp string, and simplify the function. I assume err_msg will always be longer than 25 characters :)
Code:void UdpServer::InsertIn2DB(std::string err_msg){
Socket2DB<<err_msg.c_str();
// substr yapilacak burda
err_msg=err_msg.substr(25);
}