Reputation: 1104
How can I convert a string
in to char *
I was using the following method, but it doesn't work.
At runtime it gives me the following error:
Run-Time Check Failure #3 - The variable 'url' is being used without being initialized.
eventhough I have initialized it as shown in the code bellow. Can you please show me with an example?
char* url;
sup = "news"
sup="http://www."+sup+"yahoo.com";
strcpy(url, sup.c_str());
I am using Microsoft Visual Studio 2010, C++ in console
Upvotes: 3
Views: 21742
Reputation: 31
In my opinion there are two ways of doing this.
Using &-operator (ampersand)
string str = "hello";
char *p;
p = &str[0];
Using c_str() function
string s = "hello";
const char *p;
p = s.c_str();
I have tested both and they both are working. Kindly correct me if I am wrong.
Upvotes: 3
Reputation: 258548
strcpy
doesn't allocate memory for you, you must do it yourself, remembering to leave space for the null termination character:
char* url = new char[sup.length()+1];
or
char url[sup.length()+1];
//...
strcpy(url, sup.c_str());
In the first case, don't forget to delete[]
the array. The second case will only work if your compiler supports C99 variable-length arrays as an extension.
Upvotes: 5
Reputation: 5219
std::string sup = "news";
sup="http://www."+sup+"yahoo.com";
char* url = new char[sup.length()+1];
url = const_cast<char*>(sup.c_str());
url[sup.length()] = '\0';
std::cout<<url; //http://www.newsyahoo.com
Notice the '\0'
at the end of url.
Upvotes: 0
Reputation: 31425
If you really need a writable buffer of char that is copied from the contents of a std::string, you can use std::vector<char>
std::vector<char> urlAsVec( sup.begin(), sup.end() );
urlAsVec.push_back( '\0' );
char * url = &urlAsVec[0]; // pointer you can safely write to
You can also initialize the vector this way:
std::vector<char> urlAsVec( sup.c_str(), sup.c_str() + sup.size() + 1 );
which will write your null-terminator for you too.
Another alternative:
std::vector<char> urlAsVec( sup.size() + 1 );
sup.copy( &urlAsVec[0], sup.size() );
Note that your vector will automatically initialize all its elements to 0 so the null terminator will be there even though sup.copy() doesn't write it. You can use sup.copy( ptr, len)
instead of strcpy anyway, which is slightly safer in that you can specify the buffer size (although strncpy would allow that too), although you will still have to write the null terminator manually (or have it already allocated).
For example if you use this:
char url[ BUFLEN ] = {0};
sup.copy( url, BUFLEN-1 );
for some fixed value BUFLEN you will get a copy or partial copy of the source string written into your buffer. Note that my initializer ensures all your unwritten bytes are 0.
Upvotes: 1
Reputation: 9526
strcpy
is unsafe as buffer overflow may occur. Use strncpy
where you're providing exactly number of bytes to copy. You need to allocate memory for the destination buffer and add trailing \0
:
std::string strInsert("news");
std::string sup("http://www.");
sup += strInsert + "yahoo.com";
char* url = new char[sup.length() + 1];
strncpy(url, sup.c_str(), sup.length());
url[sup.length()] = '\0';
// ... use url
delete[] url;
Upvotes: 2
Reputation: 7672
Copying from sup
to url
doesn't mean you have initialized url
.
Initializing url
means allocating memory for it. Like this:
url = new char[size];
Upvotes: 0
Reputation: 204746
char* url = new char[100];
You have to allocate memory for your char array first.
Upvotes: 1