C 从不兼容的指针类型传递参数
这是一个方法,其指针正在dll\u wifi\u状态结构中传递C 从不兼容的指针类型传递参数,c,pointers,arguments,incompatibletypeerror,C,Pointers,Arguments,Incompatibletypeerror,这是一个方法,其指针正在dll\u wifi\u状态结构中传递 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_e
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;
};
在另一个文件中,我调用这个方法
static void up_from_dll(int link, const char *data, size_t length)
{
//some code here
}
有没有一种方法可以在不出错的情况下向该方法添加另一个参数???我对指针很不在行:(
任何帮助都将不胜感激
第153行:
ap.c:153: warning: passing argument 2 of ‘dll_wifi_new_state’ from incompatible pointer type
方法
dll_states[link].data.wifi = dll_wifi_new_state(link,
up_from_dll,
true /* is_ds */);
除了将新参数添加到up\u from\u dll之外,我没有更改任何其他内容。第二个参数到
dll\u wifi\u new\u state
是up\u from\u dll\u fn\u ty callback
它现在不在代码列表中,但是up\u from\u dll\u fn\u ty
是一个类型定义,表示up\u from\u dll\u fn\u ty
是一个带有特定参数的函数指针(不包括int seq
)
当您使用不同的参数从\u dll更新up\u时,它不再匹配由up\u from\u dll\u fn\u ty
指定的类型,并应作为dll\u wifi\u new\u state
的第二个参数。您需要将该参数添加到up\u from\u dll\u fn\u ty
中,您应该很好
如果你把的定义从_dll _fn _ty
上传上来,这将使问题包含所有信息,如果你还需要的话,请允许我为你提供更多帮助
您正在寻找以下内容:
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;
}
并将其更改为
typedef void (*up_from_dll_fn_ty)(int link, const char *data, size_t length);
下面是一个问题的链接,其中包含有关为函数指针创建typedef的详细信息:
第153行:dll_状态[link].data.wifi=dll_wifi_新状态(link,up_from_dll,true/*is_ds*/);struct dll_wifi_状态*dll_wifi_新状态(int link,up_from_dll_fn_ty callback,bool is_ds){if(link>nodeinfo.nlinks | | | linkinfo[link].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\u callback=callback;state->is\u ds=is\u ds;return state;}dll\u wifi\u state从\u dll()调用up\u将我的数据包从数据链路层传递到下一层。“seq”数字是在发送到下一层之前检查接收到的消息是否是预期的消息。非常确定我已经回答了这个问题。我处理函数指针已经有一段时间了,所以花了一段时间才弄清楚这一点:)不,没有(*up_from_dll_fn_ty)整个项目中的任何位置。据我所知,up_from_dll_fn_ty nl_回调指up_from_dll()方法。我现在明白了,并用清晰的答案更新我的答案。
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);