Almost every C++ libcurl example goes like this:
std::string response_string;
curl_easy_setopt(curl, CURLOPT_URL, "http://some url");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, getAnswerFunction);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string);
Question is why does &response_string
work where a C char*
is expected?
Also, why does response_string.c_str()
core dump if I use that instead of &response_string
?
PS The above snippet is taken from this question but most examples use the same method
Question is why does
&response_string
work where a Cchar*
is expected?
A C char*
is not necessarily expected. Whatever you pass is later given as an argument to the WRITEFUNCTION
. It could be char*
, FILE*
, std::string*
, or any other pointer type depending on how WRITEFUNCTION
is implemented.
Also, why does
response_string.c_str()
core dump if I use that instead of&response_string
?
Probably due to passing a char*
where a std::string*
is expected.