Reputation: 646
I am currently working on a program about CURL. I have written the following codes to add a custom header:-
struct curl_slist *chunk = NULL;
chunk = curl_slist_append(chunk, "Another: yes");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
The above codes are fine, but if I change the codes to following, I found that the header sent doesn't contain Another: yes
:
void add_header(CURL *c, struct curl_slist *h){
h = curl_slist_append(h, "Another: yes");
}
struct curl_slist *chunk = NULL;
add_header(curl, chunk);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
What is the problem in my second piece of code?
Upvotes: 2
Views: 1513
Reputation: 29966
The problem is where you are passing the pointer to chunk
to a function, and then assigning a different value to it. You are passing the pointer itself by copy, which means that the h
inside the function, is a different pointer than the chunk
outside of it (yes, they both point to the same location, but it doesn't matter since you are changing the value of the pointer itself, not the memory it points to). To change that, pass the pointer by reference:
void add_header(CURL *c, struct curl_slist *&h){ //note the *&
h = curl_slist_append(h, "Another: yes");
}
Upvotes: 1