覆盖函数指针引发错误:不可分配非对象类型 [英] Overwriting function pointer raises error: non-object type is not assignable

查看:198
本文介绍了覆盖函数指针引发错误:不可分配非对象类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在开发一个猴子补丁c函数调用的演示,其思想是您有一个共享的c文件(例如:lib.c),该文件在头文件中有一个名为void foo()的导出函数lib.h.

I'm currently working on a demo to monkey patch c function calls, the idea is that you have a shared c file (eg: lib.c), which has one exported function called void foo() in the header file lib.h.

当前,我正在尝试执行以下操作:

Currently I'm trying to do the following:

#include <stdio.h>
#include "lib.h"

void (*foo_original)() = NULL;
void foo_patch()
{    
    puts("Before foo!");
    (*foo_original)();
    puts("Before foo!");
}

int main()
{
    foo_original = &foo;
    foo = &foo_patch;

    // Somewhere else in the code
    foo();
}

但是这给了我以下错误:

However this gives me the following error:

error: non-object type 'void ()' is not assignable

有人知道我该如何解决吗?谢谢

Does anyone know how I could fix this? Thanks

推荐答案

此行:

foo = &foo_patch;

不重新分配函数指针,而是尝试重新分配函数foo本身的地址,这是不允许的,因为这是

Is not reassigning the function pointer, but trying to reassign the address of the function foo itself, which is not permissible because that's an r-value.

如果将指针foo_original重新分配为指向foo_patch,则将得到一个无限递归循环,因为foo_patch会调用foo_original指向的函数.

If you were to reassign the pointer foo_original to point to foo_patch instead, you'd get an infinite recursion loop because foo_patch calls the function pointed to by foo_original.

这篇关于覆盖函数指针引发错误:不可分配非对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆