how can i substr char * compiler gives error for type
tahnx for ur help
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;
Last edited by qqqqxxxx; 04-21-2006 at 05:04 AM.
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!
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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>.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
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); }