Thread: char * substr

  1. #1
    Registered User
    Join Date
    Apr 2006
    Location
    Turkey/Ankara
    Posts
    3

    char * substr

    how can i substr char * compiler gives error for type
    tahnx for ur help

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    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.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Location
    Turkey/Ankara
    Posts
    3

    ok i convereted char* -> str i made manupularion but

    now i have to convert it to str->char*
    i used `=` it gives error
    thanx for ur help

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    34,306
    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.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    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();

  6. #6
    Registered User
    Join Date
    Apr 2006
    Location
    Turkey/Ankara
    Posts
    3

    Revise

    OS : Linux ZenWalk
    Compliler : g++
    c++

    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(); 
    }
    error:
    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

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    if you want to modify the passed string use strcpy. assignement doesn't have any effect because the pointer err_msg is a copy

    Code:
    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()); 
    }
    Kurt

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,052
    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.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    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);
        
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 10:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM