我正在尝试将结构传递给使用 curl_easy_setopt 的回调集。这是结构:
typedef struct gfcontext_t {
int sockfd;
char requested_path[1024];
char path[1024];
} gfcontext_t;
struct gfcontext_t ctx;
我已经用这两行设置了回调:
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ctx);
回调本身看起来像这样:
size_t write_callback(void *buffer, size_t size, size_t nmemb, void *userp) {
printf("userp: %s\n", userp.requested_path);
}
机器说“请求成员‘requested_path’不是结构或 union 。”所以我猜 struct (ctx) 没有被正确传递。谁能指出我正确的方向?
最佳答案
userp 是一个指针。您应该将其类型转换为 gfcontext_t *
。
printf("userp: %s\n", ((gfcontext_t *)userp)->requested_path);
关于c - 将结构传递给采用 void* 类型变量的回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42941460/