incompactable pointer type passing int to parameter of type void(*)(void) in coretelephony obj c

╄→гoц情女王★ 提交于 2019-12-13 07:06:07

问题


The function defined in coreTelephony.h is

void _CTServerConnectionRegisterForNotification(CTServerConnectionRef,void *,void(*callback)(void));

Then I tried to call this function

int x = 0; //placehoder for callback
_CTServerConnectionRegisterForNotification(conn,kCTCellMonitorUpdateNotification,&x);

it return the error

incompatible pointer type passing int to parameter of type void(*)(void) in coretelephony obj c

What am I missing?


回答1:


The third argument of _CTServerConnectionRegisterForNotification is a function pointer, and you pass a pointer to an int. Even if you succeeded to do it using casts, later when the connection needs to notify you it will try to use the value you passed as a function, and since it is not a function, you most likely will see a crash.

Use a function not an int:

void callback()
{
}

and then in your current code:

_CTServerConnectionRegisterForNotification(conn,kCTCellMonitorUpdateNotification,&callback);



回答2:


Here, the third argument to _CTServerConnectionRegisterForNotification() is meant to be a pointer to a function having

  • void return type
  • Accepting no parameter.

You cannot pass the address of an int in that case. It is wrong and will cause undefined behavior.



来源:https://stackoverflow.com/questions/30275026/incompactable-pointer-type-passing-int-to-parameter-of-type-voidvoid-in-cor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!