简体   繁体   English

什么是(void(**)())以及如何键入它?

[英]What is (void (**) ()) and how to typedef it?

In an embedded code I have to understand, there's this line of code : 在我必须理解的嵌入式代码中,有这一行代码:

*((void (**) ()) 0x01) = c_int01; /* Write the interrupt routine entry */

I can grasp the fact that you setup the interruption vector with the function pointer c_int01 , but I can't figure what kind of cast (void (**) ()) refers to. 我可以掌握使用函数指针c_int01设置中断向量这一c_int01 ,但我无法确定哪种类型的c_int01 (void (**) ())引用。 I know the standard function pointer notation (void (*)()) but not the other one. 我知道标准函数指针表示法(void (*)())但不知道另一个。

I tried to refactor the code so that it looked a bit more readable like this: 我试图重构代码,使它看起来更像这样:

// header
typedef void (*interrupt_handler)(); // prototype of an interruption handler
#define INTERRUPT_VECTOR 0x01
#define SET_INTERRUPT_HANDLER( handler ) *((interrupt_handler) INTERRUPT_VECTOR) = (handler)

// code
SET_INTERRUPT_HANDLER( c_int01 );

But the embedded compiler whines about the LHS not beeing an object. 但嵌入式编译器抱怨LHS不是一个对象。

Anybody know what this notation signifies? 有谁知道这个符号表示什么? (void (**)())

// EDIT: //编辑:

For those interrested, I would understand this much better: 对于那些感兴趣的人,我会更好地理解这一点:

*( (void (*)())* 0x01) = c_int01;

It's a pointer-to-pointer-to-function. 它是一个指向函数的指针。

So the cast converts the integer 0x01 to the address of a function pointer having type (void (*)()) 所以强制转换将整数0x01转换为具有类型(void (*)())的函数指针的地址

You could rewrite it: 你可以改写它:

typedef void (*interrupt_handler)();
*((interrupt_handler*) 0x01) = c_int101;

(void (**) ()) is a pointer to a function pointer. (void (**) ())是指向函数指针的指针。

( (void (*)()) is a pointer to a function, so adding a star adds a level of indirection.) (void (*)())是一个指向函数的指针,因此添加一个星形会增加一个间接级别。)

You need to say: 你需要说:

*((interrupt_handler*) INTERRUPT_VECTOR) = (handler)

That reads, "Treat INTERRUPT_VECTOR as a pointer to a function pointer, and set its value to handler ." 它写道: “将INTERRUPT_VECTOR视为指向函数指针的指针,并将其值设置为handler 。”

Here's what the ever-useful cdecl says about the core of that expression, the (void (**) ()) : 这是有用的cdecl关于该表达式核心的说法(void (**) ())

cast unknown_name into pointer to pointer to function returning void 将unknown_name转换为指向函数返回void的指针

So, it's a cast (as signified by the outer pair of parentheses), and the type is "pointer to pointer to function", which seems to make sense. 所以,它是一个强制转换(由外部括号表示),类型是“指向函数的指针”,这似乎是有道理的。

Cdecl would be the quicker way to know: Cdecl将是更快的了解方式:

     cast unknown_name into pointer to pointer to function returning void

The famous "spiral rule would be the next: 着名的“螺旋式规则将成为下一个:

          +-----+
          |+-+  |
          || |  V
   (void (** |)( ))
      ^   ^^||  |
      |   ||||  |
      |   ||+|  |
      |   +--+  |
      +---------+

Following the lines you read: 按照您阅读的内容:

  • a pointer to 一个指针
  • a pointer to 一个指针
  • a function returning 一个函数返回
  • void 空虚

You may visualize the setting of interrupt entry point vector as 您可以将中断入口点向量的设置可视化为

   void (*interupt_handlers)()[256] = 0;

   void set_interupt_handler(int vector, void(*handler)())
   {
       interupt_handlers[vector] = handler;
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM