2013-05-19 40 views
1
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鏈接,爲const char *的數據,爲size_t長度,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改變二話不說。

+0

我現在明白了,並用清晰的答案更新我的答案WER。 – xaxxon

回答

2

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

下面是有關於函數指針創建類型定義良好的信息問題的鏈接:

Understanding typedefs for function pointers in C

+0

line 153:dll_states [link] .data.wifi = dll_wifi_new_state(link, up_from_dll, true/* is_ds * /); – user2398101

+0

結構dll_wifi_state * dll_wifi_new_state(INT鏈路, up_from_dll_fn_ty回調, 布爾is_ds) { 如果(聯繫> nodeinfo.nlinks || linkinfo [鏈接] .linktype!= LT_WLAN) 返回NULL; struct dll_wifi_state * state = calloc(1,sizeof(struct dll_wifi_state)); if(state == NULL) return NULL; //初始化結構的成員。 state-> link = link; state-> nl_callback = callback; state-> is_ds = is_ds; 返回狀態; } – user2398101

+0

dll_wifi_state調用up_from_dll()將數據包從數據鏈路層傳遞到下一層。 'seq'數字是在接收下一層之前檢查收到的味精是否是預期的味精。 – user2398101

相關問題