0
votes

I am working on STM8S with IAR Workbench.

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"

I search on the web and I find a sample for AVR that I show below:

typedef void (*MyFunction)(void); 

It also doesn't work.

Any idea for fix it.

1
Since the problem is in an assignment to a function pointer, you need to show something like that to produce a minimal reproducible example. And don't neglect that u8 type in the error. - StoryTeller - Unslander Monica
Are you perhaps compiling as C++? - John Bollinger
There exists no case in C where you should use an empty parameter list (). Particularly not when dealing with function pointers. - Lundin

1 Answers

2
votes

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.

Assuming u8 is uint8_t AKA unsigned char, void () is NOT compatible with void (u8).

6.7.6.3p15 (with emphasis):

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; 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:

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

doing the same with u8 / unsigned char is not permissible:

(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. (Note that function pointers are freely inter-castable but you need to cast to the proper type before calling the function).