3

I have seen many similar questions but none that seem to be working for my code, I think I am overlooking something basic, maybe you can help.

Right now I'm constructing a string to be sent as a request to the winsock send function, which requires a char [] to be sent. I need to convert my std::string into a char array (char []).

Currently this is the request that works:

char request [] = "GET /gwc/cgi-bin/fc?client=udayton0.1&hostfile=1HTTP/1.0\nHost:www.gofoxy.net\n\n";

But I need to change a string of a request to the same data structure.

I appreciate any help!

edit:

I can convert a string to a char *, but how can I use that to get a character array? I hope I'm not making it more confusing. Here are two attempts that produce char star's that aren't compatible with the send request when I run:

//convert request into a char[]
//char *req_ch = new char[req_str.size()+1];
//hostCh[req_str.size()] = 0;
//memcpy(req_ch, req_str.c_str(), req_str.size());

//char * req = new char[req_str.size() +1];
//std::copy(req_str.begin(), req_str.end(), req);
//req[req_str.size()] = '\0';
3
  • 3
    Isn't c_str() what you are looking for, which returns a pointer? Commented Sep 11, 2012 at 1:52
  • So a string to non-const character array? I'd go for a vector constructed with iterators and using vector::data(). Commented Sep 11, 2012 at 1:52
  • 1
    @gosly, An array will decay to a pointer when passed into a function, so it doesn't matter if you pass in a pointer. Commented Sep 11, 2012 at 2:04

2 Answers 2

8

If you have some legacy C-style API with a signature like this, where it is expecting a null terminated C-style string:

void foo(const char* data);

then you can typically do something like:

std::string s("my string data");
foo(s.c_str());

If you really need an array, or a non-const version of the data, so that the API is this:

void foo(char* data, std::size_t len);  // or foo(char[] data, std::size_t len);

Then you could do something like this:

std::string s("my string data");
std::vector<char> v(s.begin(), s.end());
foo(&v[0], v.size());    // or foo(v.data(), v.size()); in C++11
Sign up to request clarification or add additional context in comments.

Comments

2

Do you really need char[] or will char const* do?

If the latter, c_str() will do what you want.

If the former then you'll need to copy the string into something that does what you want:

std::vector<char> buff(str.size() + 1); // initializes to all 0's.
std::copy(str.begin(), str.end(), buff.begin());
fun_call(&buff[0]);

Chances are that you only need the c_str version. If you're needing this second version then you're reading and probably, hopefully, providing a buff max size.

2 Comments

Just construct the vector with str.begin(), str.end() (or std::begin and std::end). Since C++11, it also has data(), which is meant for accessing the pointer.
It'd be interesting to see which is faster what with needing to append a '\0'. An improvement to both might be to use str.c_str(), str.c_str() + str.size + 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.