質問

    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;

};

This is the method whose pointer is being passed in the dll_wifi_state struct.

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

In other file, I am calling this method

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");
}

It works fine like this, but I want to add another argument i.e. up_from_dll((int link, const char *data, size_t length, int seq). And as soon as I add this argument, following error starts coming up

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

Is there a way of adding another argument to that method without getting error ??? I am really bad with pointers :(

Any help would be much appreciated.

Line 153 :

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

And method

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;
}

I haven't changed anything else apart from adding the new parameter to up_from_dll.

役に立ちましたか?

解決

The second parameter to dll_wifi_new_state is up_from_dll_fn_ty callback.

It's not in your code listing right now, but up_from_dll_fn_ty is a typedef saying that the up_from_dll_fn_ty is a function pointer with specific parameters (which don't include int seq)

When you updated up_from_dll with different parameters, it no longer matches the type specified by up_from_dll_fn_ty and expected as the second parameter for dll_wifi_new_state. You'll need to add the parameter to up_from_dll_fn_ty and you should be good.

If you post the definition of up_from_dll_fn_ty, it would make the question have all the information and allow me to help you more if you still need it.

You're looking for something like:

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

and change it to

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

Here's a link to a question that has good information about creating typedefs for function pointers:

Understanding typedefs for function pointers in C

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top