【问题标题】:Passing a struct to a callback that takes a variable of type void*将结构传递给采用 void* 类型变量的回调
【发布时间】:2017-03-22 02:06:51
【问题描述】:

我正在尝试将结构传递给使用 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_pa​​th’。”所以我猜结构(ctx)没有被正确传递。谁能指出我正确的方向?

【问题讨论】:

  • 类型是void *,而不是void

标签: c struct


【解决方案1】:

userp 是一个指针。您应该将其类型转换为 gfcontext_t *

printf("userp: %s\n", ((gfcontext_t *)userp)->requested_path);

【讨论】:

  • 当然。我仍然无法完全理解指针。谢谢。
猜你喜欢
  • 1970-01-01
  • 2012-05-15
  • 2013-10-19
  • 2011-11-13
  • 2014-08-20
  • 1970-01-01
  • 2021-08-28
  • 1970-01-01
  • 2016-08-09
相关资源
最近更新 更多