简体   繁体   中英

What is libcurl doing with the third parameter on curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, function) in C

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM