覆盖函数指针会引发错误:非对象类型不可分配

时间:2017-06-18 19:53:00

标签: c function-pointers

我目前正在开发一个针对补丁c函数调用的演示,想法是你有一个共享的c文件(例如:from random import * def doorPriceRandomizer(): door1 = randint(0,2) #If a door is defined 0, it has a price in it door2 = randint(0,2) #If a door is defined either 1 or 2, it has a goat in it. door3 = randint(0,2) while door2 == door1: door2 = randint(0,2) while door3 == door2 or door3 == door1: door3 = randint(0,2) return door1,door2,door3 #This random placement generator seems to be working fine. while True: loopStart = 0 amountWin = 0 amountLose = 0 try: loopEnd = int(input("How often would you like to run this simulation: ")) if loopEnd < 0: raise ValueError doorChangeUser = int(input("[0] = Do not change door; [1] = Change door: ")) if doorChangeUser not in range(0,2): raise ValueError except ValueError: print("Invalid input. Try again.\n") else: while loopStart != loopEnd: gameDoors = doorPriceRandomizer() inputUser = randint(0,2) if doorChangeUser == 0: if gameDoors[inputUser] == 0: amountWin += 1 loopStart += 1 else: amountLose += 1 loopStart += 1 elif doorChangeUser == 1: ChangeRandom = 0 while gameDoors[ChangeRandom] == gameDoors[inputUser]: ChangeRandom = randint(0,2) if gameDoors[ChangeRandom] == 0: amountWin += 1 loopStart += 1 else: amountLose += 1 loopStart += 1 print("Win amount: ",amountWin,"\tLose amount: ",amountLose) ),它有一个名为{{1}的导出函数}在头文件lib.c中。

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

void foo()

然而,这给了我以下错误:

lib.h

有谁知道如何解决这个问题?感谢

1 个答案:

答案 0 :(得分:2)

这一行:

foo = &foo_patch;

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

如果要重新指定指针foo_original以指向foo_patch,则会得到无限递归循环,因为foo_patch会调用{{1}指向的函数}}