Thread: from const char to char

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    20

    from const char to char

    Hi All

    I'm writing a tool that uses C libraries.

    Which means that I have to convert strings into 'const char*'s

    So, this is how I think a string is converted into a char:
    Code:
    const char* c = x.c_str ;
    The problem I have now is that my C library functions need a char* not a const char*..

    Any suggestions what the best way is to go from a string to a char* ?

    Thanks a lot
    LuCa

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should copy over the characters (anyway).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    string to a char*
    strcpy() will make a copy that you can alter.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If the size of the string is variable, you will probably need a variable length character array. In that case, use a vector so that it will handle memory management for you. Then pass the vector to the code that expects a char*:
    Code:
    string s = "Some string.";
    vector<char> buf(s.begin(), s.end());
    buf += '\0';
    SomeCFunction(&buf[0], s.size()); // SomeCFunction takes a char* and an int size.
    That code isn't tested, but should be basically correct.

    (See 7stud's post below for corrections.)
    Last edited by Daved; 01-17-2007 at 12:21 PM.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Nice. I was trying to figure out how to handle the problem of variable lengths. Here's a tested version of Daved's solution:
    Code:
    void someCFunction(char* str)
    {
    	cout<<str<<endl;
    }
    
    int main ()
    {
    	string s = "Some string.";
    	vector<char> buf(s.begin(), s.end());
    	buf.push_back('\0');
    	
    	someCFunction(&buf[0]);
    
    	return 0;
    }

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    20
    thanks a lot, this is all I need!!

    The vector seems perfect!

    LuCa

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I guess this would be a better test:
    Code:
    void changeIt(char* str)
    {
    	str[0] = 'X';
    }
    
    int main ()
    {
    	string s = "Some string.";
    	vector<char> buf(s.begin(), s.end());
    	buf.push_back('\0');
    	
    	changeIt(&buf[0]);
    	cout<<&buf[0]<<endl;
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM