【问题标题】:incompactable pointer type passing int to parameter of type void(*)(void) in coretelephony obj c将int传递给核心电话obj c中的void(*)(void)类型参数的不可压缩指针类型
【发布时间】:2015-05-16 11:22:57
【问题描述】:

coreTelephony.h中定义的函数是

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

然后我尝试调用这个函数

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

返回错误

不兼容的指针类型将 int 传递给核心电话 obj c 中 void(*)(void) 类型的参数

我错过了什么?

【问题讨论】:

  • 你为什么要恢复之前的编辑?
  • 您在哪里找到了该声明?错了

标签: c++ c core-telephony


【解决方案1】:

_CTServerConnectionRegisterForNotification 的第三个参数是一个函数指针,你传递一个指向int 的指针。即使您使用强制转换成功了,稍后当连接需要通知您时,它也会尝试使用您作为函数传递的值,并且由于它不是函数,您很可能会看到崩溃。

使用非 int 函数:

void callback()
{
}

然后在您当前的代码中:

_CTServerConnectionRegisterForNotification(conn,kCTCellMonitorUpdateNotification,&callback);

【讨论】:

    【解决方案2】:

    这里,_CTServerConnectionRegisterForNotification() 的第三个参数是指向一个函数的指针

    • void 返回类型
    • 不接受任何参数。

    在这种情况下,您不能传递int 的地址。这是错误的,会导致undefined behavior

    【讨论】:

    • 感谢您的回复...那我该如何传递 int 变量?
    • @sai 它被称为reply,而不是replay。 :-) 你为什么首先传递一个指向int 的指针?
    • :) 我刚试过 _CTServerConnectionRegisterForNotification(conn,kCTCellMonitorUpdateNotification,x);但它返回相同的警告
    • 这是我的代码 void register_notification(){ if (!mach_port || !conn) return; void *libHandle = dlopen("/System/Library/Frameworks/CoreTelephony.framework/CoreTelephony", RTLD_LOCAL | RTLD_LAZY);无效 *kCTCellMonitorUpdateNotification = dlsym(libHandle, "kCTIndicatorsSignalStrengthNotification"); if(kCTCellMonitorUpdateNotification== NULL) NSLog(@"找不到 kCTCellMonitorUpdateNotification");诠释 x = 0; //回调的占位符 _CTServerConnectionRegisterForNotification(conn,kCTCellMonitorUpdateNotification,&x); } 最后一行返回错误
    • @sai 你没有明白这一点。为什么你要传递的东西不是函数指针
    猜你喜欢
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 2021-06-29
    • 1970-01-01
    • 2022-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多