By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 1,005 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

Converting std::string to char*

P: n/a
Hi,
This was the routine I wrote earlier to convert a C++ string to a
char array.
But I found that the char* array consisted only of junk after returning

from the below function.
int convertStringToChar(string& str, char* data)
{
data = (char*)malloc(str.length() * sizeof(char));
memset((void*)data,0,sizeof(da*ta));
strcpy(data,str.c_str());
return 0;
}
However, I replaced it with the following function and it worked just
fine.

int convertStringToChar(string& str, char** data)
{
*data = (char*)malloc(str.length() * sizeof(char));
memset((void*)*data,0,sizeof(d*ata));
strcpy(*data,str.c_str());
return 0;
}
1. Can you tell me the reason?
2. Is the second routine defect free? Note that I'm not terminating the

char* array with a "\0" anywhere.

FYI, the compiler being used is gcc 2.96.
Thanks and Regards,
Karthik.

Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
I don't know about all that, but this solution worked just fine for me:

#include <string>
#include <algorithm>
#include <iostream>
using namespace std;

char *convertStringToChar(const string &str)
{
char *retPtr(new char[str.length() + 1]);

copy(str.begin(), str.end(), retPtr);
retPtr[str.length()] = '\0';

return retPtr;
}

int main() //test main
{
string d("acbs");

char *dPtr(convertStringToChar(d));

cout << d << endl;
cout << dPtr << endl;
}
Jul 23 '05 #2

P: n/a
James Aguilar wrote:
I don't know about all that, but this solution worked just fine for me:

#include <string>
#include <algorithm>
#include <iostream>
using namespace std;

char *convertStringToChar(const string &str)
{
char *retPtr(new char[str.length() + 1]);

copy(str.begin(), str.end(), retPtr);
retPtr[str.length()] = '\0';
I think you could also replace those two lines with

strcpy(retPtr, str.c_str());

(after including <cstring>, of course)

return retPtr;
}

int main() //test main
{
string d("acbs");

char *dPtr(convertStringToChar(d));

cout << d << endl;
cout << dPtr << endl;
}


You might want to remember to 'delete[] dPtr' after you're done with it.

V
Jul 23 '05 #3

P: n/a
On Fri, 15 Apr 2005 09:32:05 -0700, ka**********@gmail.com wrote:
int convertStringToChar(string& str, char* data)
{
data = (char*)malloc(str.length() * sizeof(char));
memset((void*)data,0,sizeof(da*ta));
strcpy(data,str.c_str());
return 0;
}

This will not work because the parameter "data" is being passed by value
and any changes to it in your function will not affect the caller. You
also have a memory leak as the memory allocated by malloc() in this
function cannot be freed. This is the same reason why the following will
always print '10' not '20':

#include <iostream>

void foo(int a)
{
a = 20 ;
return ;
}

int main()
{
int b = 10 ;
foo(b) ;
std::cout << b << std::endl ;
return 0 ;
}

Thanks,
--
CrayzeeWulf

Jul 23 '05 #4

P: n/a
>> copy(str.begin(), str.end(), retPtr);
retPtr[str.length()] = '\0';


I think you could also replace those two lines with

strcpy(retPtr, str.c_str());


It is not the same. What if str contains multiple embedded '\0' ?
The former will handle that. strcpy() won't. It stops at the first '\0'.

Stephen Howe
Jul 23 '05 #5

P: n/a
Stephen Howe wrote:
copy(str.begin(), str.end(), retPtr);
retPtr[str.length()] = '\0';


I think you could also replace those two lines with

strcpy(retPtr, str.c_str());


It is not the same. What if str contains multiple embedded '\0' ?
The former will handle that. strcpy() won't. It stops at the first
'\0'.


And what use would the resulting pointer would be if it contains
multiple embedded null chars? The first embedded null character
will terminate it to any of C string functions, for example.
Jul 23 '05 #6

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:B4********************@comcast.com...

And what use would the resulting pointer would be if it contains
multiple embedded null chars? The first embedded null character
will terminate it to any of C string functions, for example.


Also, the OP specifically says the string contains no null characters.

- JFA1
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.