C中的双重功能间接 [英] Double function indirection in C
问题描述
我正在为ARM Cortex-M0 CPU编写引导加载程序.我需要将IRQ转发到目标应用程序,不幸的是,此CPU中的IRQ向量位于固定地址,并且无法重定位它,因此我需要一些技巧.
I am writing a bootloader for an ARM Cortex-M0 CPU. I need to forward the IRQs to the target app, unfortunately the IRQ vector in this CPU is at fixed address and I cannot relocate it, so I need a bit of trickery.
我知道目标应用程序的IRQ地址的存储地址. 这么说吧,在地址0x5008处是void x_IRQHandler(void)(目标应用)的地址
I know the address where the addresses of the target app's IRQs are stored. So let's say, at address 0x5008 there's the address of void x_IRQHandler(void) (target app)
我想做的是:
- 在启动时保存所有x_IRQHandler()的地址(这样就不会在运行时对其进行计算);
- 只要引导加载程序收到IRQ调用,就从目标应用程序的地址中调用相关的IRQ函数.
这是我在引导程序中所做的,我找不到正确的语法.
This is what I have done in the bootloader, I can't find the right syntax for it.
//pointers to x_IRQHandler() functions
void(* x_IRQ_addr)(void);
x_IRQ_addr = (void(*)(void))(0x5008);
//here I should call the function pointed by x_IRQ_addr; but this syntax is not valid
void x_IRQ_Handler(void)
{
*x_IRQ_addr();
}
我尝试了不同的方法但均未成功,该如何解决?
I have tried different approaches with no success, how can I fix that?
推荐答案
函数调用运算符()
的优先级高于间接运算符*
.
The function call operator ()
has higher precedence than the indirection operator *
.
尝试使用(*x_IRQ_addr)();
而不是*x_IRQ_addr();
.
您也可以将其写为x_IRQ_addr();
,因为*x_IRQ_addr
是函数指定符,它将被转换为指向()
运算符的操作数的函数的指针,因此取消引用就浪费了.
You can also write it as x_IRQ_addr();
because *x_IRQ_addr
is a function designator and it will be converted to the pointer pointing the function for operand of ()
operator, so the dereference goes to waste.
这篇关于C中的双重功能间接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!