简体   繁体   English

函数指针IAR(typedef void)

[英]Function pointer IAR ( typedef void )

I am working on STM8S with IAR Workbench. 我正在使用IAR Workbench开发STM8S。

My code 我的密码

typedef     void    (*MyFunction)(); 

and I got these errors: 我得到了这些错误:

Error[Pe513]: a value of type "void (*)(u8)" cannot be assigned to an entity of type "MyFunction" 错误[P​​e513]:无法将类型“ void(*)(u8)”的值分配给类型“ MyFunction”的实体

I search on the web and I find a sample for AVR that I show below: 我在网上搜索,然后找到下面显示的AVR示例:

typedef void (*MyFunction)(void); 

It also doesn't work. 它也不起作用。

Any idea for fix it. 任何修复它的想法。

For pointers to be assignable they need to point to compatible types (and the type pointed to by the left-hand side needs to have all the qualifiers of the type pointed to by the right-hand side). 对于可分配的指针,它们需要指向兼容的类型(并且左侧指向的类型需要具有右侧指向的所有类型的限定符)。 See 6.5.16.1p1 . 参见6.5.16.1p1

Assuming u8 is uint8_t AKA unsigned char , void () is NOT compatible with void (u8) . 假设u8uint8_t AKA unsigned char ,则void ()void (u8)不兼容。

6.7.6.3p15 (with emphasis): 6.7.6.3p15 (重点):

For two function types to be compatible, both shall specify compatible return types.146) Moreover, the parameter type lists, if both are present, shall agree in the number of parameters and in use of the ellipsis terminator; 为了使两种功能类型兼容,两者都应指定兼容的返回类型。146)此外,如果同时存在参数类型列表,则应在参数数量和省略号终止符的使用上达成一致; corresponding parameters shall have compatible types. 相应的参数应具有兼容的类型。 If one type has a parameter type list and the other type is specified by a function declarator that is not part of a function definition and that contains an empty identifier list, the parameter list shall not have an ellipsis terminator and the type of each parameter shall be compatible with the type that results from the application of the default argument promotions. 如果一种类型具有参数类型列表,而另一种类型由不属于函数定义一部分且包含空标识符列表的函数声明符指定,则参数列表不应具有省略号终止符,并且每个参数的类型均应为与应用默认参数提升的结果类型兼容。 If one type has a parameter type list and the other type is specified by a function definition that contains a (possibly empty) identifier list, both shall agree in the number of parameters, and the type of each prototype parameter shall be compatible with the type that results from the application of the default argument promotions to the type of the corresponding identifier. 如果一种类型具有参数类型列表,而另一种类型由包含(可能为空)标识符列表的函数定义指定,则两者应在参数数量上达成一致,并且每个原型参数的类型均应与该类型兼容这是由于将默认参数提升应用到相应标识符的类型而导致的。 (In the determination of type compatibility and of a composite type, each parameter declared with function or array type is taken as having the adjusted type and each parameter declared with qualified type is taken as having the unqualified version of its declared type.) (在确定类型兼容性和复合类型时,将使用函数或数组类型声明的每个参数视为已调整类型,将使用限定类型声明的每个参数视为具有其声明类型的非限定版本。)

The incompatibility is because of the promotions part. 不兼容是由于促销部分。 Character types get promoted to int so while you could do: 字符类型被提升为int因此您可以:

(void(*)()){0}=(void(*)(int)){0};
(void(*)(int)){0}=(void(*)()){0};

doing the same with u8 / unsigned char is not permissible: 不允许对u8 / unsigned char进行相同的操作:

(void(*)()){0}=(void(*)(unsigned char)){0}; //error
(void(*)(unsigned char)){0}=(void(*)()){0}; //error 

You need to make the typedef be exactly to void (*)(u8) or you need to change the target function's signature to void (int) (or void (unsigned) ) or you need to cast the function pointer. 您需要将typedef精确地设置为void (*)(u8)或者需要将目标函数的签名更改为void (int) (或void (unsigned) ),或者需要强制转换函数指针。 (Note that function pointers are freely inter-castable but you need to cast to the proper type before calling the function). (请注意,函数指针可以自由地相互广播,但是在调用函数之前,您必须将其转换为正确的类型)。

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

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