Reputation: 1126
I'm doing a simple program that gets data from a api and following the documentation I'm creating a callback function to pass to curl_easy_setopt:
size_t callback_libcurl(void *contents, size_t size, size_t nmemb, void *userp) {
size_t realsize = size * nmemb;
struct resultado *mem = (struct resultado *)userp;
char *ptr = realloc(mem->dados, mem->tamanho + realsize + 1);
if(ptr == NULL) {
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
And then passing that to:
curl_easy_setopt(handler_curl, CURLOPT_WRITEFUNCTION, callback_libcurl);
Everything is working but I have no idea why. How I am passing the callback function to the curl function like that?
The docs say the parameter can be a long, a function pointer, an object pointer or a curl_off_t.
It is none of that afaik.
The only source I could find something was:
CURL_EXTERN CURLcode curl_easy_setopt(CURL *curl, CURLoption option, ...);
on /curl/easy.h but that does not help either, I don't know what ... is. Could not find anything on it.
What am I missing?
Upvotes: 0
Views: 202
Reputation: 117771
The docs say the parameter can be a long, a function pointer, an object pointer or a curl_off_t. It is none of that afaik.
In C a function automatically decays to a function pointer when appropriate. So the following are equivalent:
curl_easy_setopt(handler_curl, CURLOPT_WRITEFUNCTION, callback_libcurl);
curl_easy_setopt(handler_curl, CURLOPT_WRITEFUNCTION, &callback_libcurl);
Therefore you are passing a function pointer, one pointing to callback_libcurl
.
Upvotes: 2