【问题标题】:passing argument from incompatible pointer type从不兼容的指针类型传递参数
【发布时间】:2013-05-19 03:17:49
【问题描述】:
    static struct dll_wifi_state **dll_states;

    enum dll_type {
      DLL_UNSUPPORTED,
      DLL_ETHERNET,
      DLL_WIFI
    };

    struct dll_state {
      enum dll_type type;

      union {
        struct dll_eth_state *ethernet;
        struct dll_wifi_state *wifi;
      } data;
    };

    static struct dll_state *dll_states = NULL;

struct dll_wifi_state {
  int link;

  // A pointer to the function that is called to pass data up to the next layer.
  up_from_dll_fn_ty nl_callback;

  bool is_ds;

};

这是在 dll_wifi_state 结构中传递其指针的方法。

static void up_from_dll(int link, const char *data, size_t length)
{
//some code here
}

在其他文件中,我正在调用这个方法

void reboot_accesspoint()
{
  // We require each node to have a different stream of random numbers.
  CNET_srand(nodeinfo.time_of_day.sec + nodeinfo.nodenumber);

  // Provide the required event handlers.
  CHECK(CNET_set_handler(EV_PHYSICALREADY, physical_ready, 0));

  // Prepare to talk via our wireless connection.
  CHECK(CNET_set_wlan_model(my_WLAN_model));

  // Setup our data link layer instances.
  dll_states = calloc(nodeinfo.nlinks + 1, sizeof(struct dll_state));

  for (int link = 0; link <= nodeinfo.nlinks; ++link) {
    switch (linkinfo[link].linktype) {
      case LT_LOOPBACK:
        dll_states[link].type = DLL_UNSUPPORTED;
        break;

      case LT_WAN:
        dll_states[link].type = DLL_UNSUPPORTED;
        break;

      case LT_LAN:
        dll_states[link].type = DLL_ETHERNET;
        dll_states[link].data.ethernet = dll_eth_new_state(link, up_from_dll);
        break;

      case LT_WLAN:
        dll_states[link].type = DLL_WIFI;
        dll_states[link].data.wifi = dll_wifi_new_state(link,
                                                        up_from_dll,
                                                        true /* is_ds */);
        break;
    }
  }

  // printf("reboot_accesspoint() complete.\n");
}

它像这样工作正常,但我想添加另一个参数,即 up_from_dll((int link, const char *data, size_t length, int seq)。一旦我添加了这个参数,就会出现以下错误

ap.c:153: warning: passing argument 2 of ‘dll_wifi_new_state’ from incompatible pointer type

有没有办法在不出错的情况下向该方法添加另一个参数???我对指针真的很不好:(

任何帮助将不胜感激。

第 153 行:

dll_states[link].data.wifi = dll_wifi_new_state(link,
                                                            up_from_dll,
                                                            true /* is_ds */);

和方法

struct dll_wifi_state *dll_wifi_new_state(int link,
                                          up_from_dll_fn_ty callback,
                                          bool is_ds)
{
  // Ensure that the given link exists and is a WLAN link.
  if (link > nodeinfo.nlinks || linkinfo[link].linktype != LT_WLAN)
    return NULL;

  // Allocate memory for the state.
  struct dll_wifi_state *state = calloc(1, sizeof(struct dll_wifi_state));

  // Check whether or not the allocation was successful.
  if (state == NULL)
    return NULL;

  // Initialize the members of the structure.
  state->link = link;
  state->nl_callback = callback;
  state->is_ds = is_ds;

  return state;
}

除了将新参数添加到 up_from_dll 之外,我没有更改任何其他内容。

【问题讨论】:

  • 我现在明白了,并用明确的答案更新我的答案。

标签: c pointers arguments incompatibletypeerror


【解决方案1】:

dll_wifi_new_state 的第二个参数是up_from_dll_fn_ty callback

它现在不在您的代码清单中,但 up_from_dll_fn_ty 是一个 typedef,表示 up_from_dll_fn_ty 是一个带有特定参数的函数指针(不包括 int seq

当您使用不同的参数更新up_from_dll 时,它不再与up_from_dll_fn_ty 指定的类型匹配,并且应该作为dll_wifi_new_state 的第二个参数。您需要将参数添加到up_from_dll_fn_ty,您应该会很好。

如果您发布up_from_dll_fn_ty 的定义,它将使问题包含所有信息,如果您仍然需要,我可以为您提供更多帮助。

你正在寻找类似的东西:

typedef void (*up_from_dll_fn_ty)(int link, const char *data, size_t length);

并将其更改为

typedef void (*up_from_dll_fn_ty)(int link, const char *data, size_t length, int seq);

这是一个问题的链接,其中包含有关为函数指针创建 typedef 的详细信息:

Understanding typedefs for function pointers in C

【讨论】:

  • 第 153 行:dll_states[link].data.wifi = dll_wifi_new_state(link, up_from_dll, true /* is_ds */);
  • struct dll_wifi_state *dll_wifi_new_state(int link, up_from_dll_fn_ty callback, bool is_ds) { if (link > nodeinfo.nlinks || linkinfo[link].linktype != LT_WLAN) return NULL;结构 dll_wifi_state *state = calloc(1, sizeof(struct dll_wifi_state));如果(状态 == NULL)返回 NULL; // 初始化结构体的成员。状态->链接=链接;状态->nl_callback = 回调;状态->is_ds = is_ds;返回状态; }
  • dll_wifi_state 调用 up_from_dll() 将我的数据包从数据链路层传递到下一层。 'seq' 号是在发送到下一层之前检查收到的 msg 是否是预期的 msg。
  • 很确定我已经回答了这个问题。自从我处理函数指针以来已经有一段时间了,所以花了一段时间才弄清楚这个:)
  • 不,整个项目中的任何地方都没有 (*up_from_dll_fn_ty) 的 typedef。据我了解,up_from_dll_fn_ty nl_callback 指的是 up_from_dll() 方法。
猜你喜欢
  • 1970-01-01
  • 2013-12-25
  • 1970-01-01
  • 2016-01-27
  • 2011-08-17
  • 2016-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多