0

I was going through a libcurl example and saw CURL *&conn being passed as one of the arguments. What does this construct mean?

Here's the code where it occurs:

// // libcurl connection initialization // 
static bool init(CURL *&conn, char *url) 
{ 
   CURLcode code; conn = curl_easy_init(); 
   if (conn == NULL) 
     { 
       fprintf(stderr, "Failed to create CURL connection\n"); 
       exit(EXIT_FAILURE); 
     } 
   code = curl_easy_setopt(conn, CURLOPT_ERRORBUFFER, errorBuffer);
   if (code != CURLE_OK) 
     { 
       fprintf(stderr, "Failed to set error buffer [%d]\n", code);
       return false; 
     } 
  return true;
}

Edit: I asked this when I was a beginner and didn't realise duplicates existed. Thanks Stack Overflow.

Abhinav Ralhan
  • 533
  • 1
  • 4
  • 13
  • 2
    Can you show some of the context? – Brian Bi Oct 15 '16 at 05:59
  • 2
    Reference to a pointer to a CURL control block. Main use would be to change which CURL control block is being pointed at inside the function and carry that change back to the caller. Beyond that, no clue what it is being used for. – user4581301 Oct 15 '16 at 06:02
  • @Brian Here's the excerpt: ` // // libcurl connection initialization // static bool init(CURL *&conn, char *url) { CURLcode code; conn = curl_easy_init(); if (conn == NULL) { fprintf(stderr, "Failed to create CURL connection\n"); exit(EXIT_FAILURE); } code = curl_easy_setopt(conn, CURLOPT_ERRORBUFFER, errorBuffer); if (code != CURLE_OK) { fprintf(stderr, "Failed to set error buffer [%d]\n", code); return false; } ` Also the link: curl.haxx.se/libcurl/c/htmltitle.html – Abhinav Ralhan Oct 15 '16 at 06:32
  • Best to add that to the question with an edit. As you can see, comments don't handle code all that well and it is easier on the brain if all question material is in the question. – user4581301 Oct 15 '16 at 06:32
  • @user4581301 that was much needed, thankyou! – Abhinav Ralhan Oct 15 '16 at 06:33
  • Can't fully explain that right now, but the gist is the pointer to CURL is updated with a CURL freshly created by the function. If the function returns true, the CURL is usable. This is one of the worst ways to do this in C++. A wrapper class with constructor and destructor so you can take advantage of RAII would be more C++ish. – user4581301 Oct 15 '16 at 06:38
  • @user4581301 think that makes a lot of sense. Appreciate the help :) – Abhinav Ralhan Oct 15 '16 at 07:07

0 Answers0