Thread: string to char converting problem

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    2

    string to char converting problem

    hi,
    first i ll want to tell you what i am planing to do:
    i want to convert a cpp string to a char array
    the string gets assembled with x = x + y and wors just fine.
    then i want to convert this string into a dynamically sized char array.

    Code:
            string sFinalQuery;
    
    	char *cQuery;   // where everything should go at the end.
     
    	sFinalQuery = "select * from gpsdestinations where name like '%";
    	sFinalQuery = sFinalQuery + sMySQLQuery;
    	sFinalQuery = sFinalQuery + "%' OR beschreibung like '%";
    	sFinalQuery = sFinalQuery + sMySQLQuery;
    	sFinalQuery = sFinalQuery + "%'";
    	
    	cout << sFinalQuery << endl; // test output of the string
    	printf("\n\nLänge Query : %d\n\n", sFinalQuery.length()); // printing string lenght
    	cQuery = (char *) malloc(sFinalQuery.length());
    	for (int i=0; i < sFinalQuery.length(); i++){
    		cQuery[i] = sFinalQuery[i];
    		printf("%c", cQuery[i]);
    	}
    	printf("\n\nLänge Query : %d\n\n", strlen(cQuery));  // printing array lenght
    at the end the lenght from the string and the array are different but they should not be.

    may some of you can post a idea why the lenghts are not the same.
    i am just going nuts with it.

    greez bergziege

    ps.: sMySQLQuery is just another string coming from an input device

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    I would suggest being careful with the use of malloc/free in C++ code (its OK for built-in types, but for anything else you should use new/delete. For the sake of consistency, you should prefer new/delete unless you have some compelling reason to do otherwise. At the moment your code looks like a fairly ugly mix of C and C++)

    you can get a const char* representation of a std::string with the string::c_str() function.

    so if you have a function with a signature requiring a const char*, pass it as so

    Code:
    void some_function(const char* );
    
    /* .... */
    
    std::string my_string("Hello, World");
    some_function( my_string.c_str() );
    
    /* .... */
    Perhaps the reason that your lengths are different is that you are allocating enough space for sFinalQuery.length() ... this will return the length of the string - however a C-style string requires a null terminating character, so you need enough space for sFinalQuery.length() + 1. At the moment I believe your program exhibits undefined behaviour.
    Last edited by Bench82; 08-07-2007 at 03:00 PM.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    2
    thanks for the fast response.
    i skipped all the dynamic stuff and now using .c_str()
    works just fine.

    thanks again for help
    greez bergziege

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. String Processing Problem
    By muffin in forum C Programming
    Replies: 0
    Last Post: 08-24-2001, 10:13 AM