Pointer of Function

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Pointer of Function

2,187 次查看
DavidoBG
Contributor II
I have some compilator errors due to pointer of function
 
void MyFunct(void)
{
.....
.....
.....
}
void main(void)
{
void (*MyPtrfunc) ();
MyPtrFunct = &MyFunct;
 
MyPtrFunct = MyPtrFunct - OffSetSW    //Compilator error desciption : Error C1829 : + - incompatible Type
 
MyPtrFunct();
 
I already use this kind of declaration with another compilator, and i have no issue!!!
Do you have any about idea about my issue?
For you comprehension : Before run this command line, i rewrite Myfunction in spectific section in flash.
In fact, i try run a function place @ specific adress. This specific adress is Default adress (cofigured by emulator) - 0x1800.
Thanks
David
 
 
 
标签 (1)
0 项奖励
回复
6 回复数

753 次查看
DavidoBG
Contributor II
OK thanks much
david
0 项奖励
回复

753 次查看
DavidoBG
Contributor II
so my line programmation becomes
 
//OffsetSW = 0x1800 may be i must cast with unsigned int???
MyPtrFunct = (char*)MyPtrFunct - OffSetSW
 
Now i don't have any compilator error but a following warning appears :
Non stardart conversion used. Is it important??
 
David
0 项奖励
回复

753 次查看
Lundin
Senior Contributor IV
Yes. Substracting one pointer from another when they aren't pointing at the same array is undefined behavior in C. Typecasting from a function pointer to any other pointer type is also undefined behavior, you can only cast to integer.

The warning means that the code isn't legal C but that the compiler has made a non-standard extension to allow it. So the code will be non-portable.

The quick & dirty way around it is to cast both pointers to integer, then substract, then cast the result back to a function pointer.
0 项奖励
回复

753 次查看
CrasyCat
Specialist III
Hello
 
This is an ANSI C limitation you cannot use pointer arithmetic on function pointer.
 
Keep in mind that if you have a pointer to an int, ptr - 2 is in fact subtracting 4 (2 * sizeof(int)) from the pointer.
You cannot use sizeof on a function pointer, so you will not be able to use pointer arithmetic on such a pointer either
 
So you need to cast the function pointer to a pointer to char and do the arithmetic.
 
CrasyCat
0 项奖励
回复

753 次查看
Lundin
Senior Contributor IV
: This is an ANSI C limitation you cannot use pointer arithmetic on function pointer.

I very much doubt this is true. Could you please cite the ISO standard?

It is however indeed true that you can't use sizeof on function pointers.
0 项奖励
回复

753 次查看
CompilerGuru
NXP Employee
NXP Employee
I would not call it a limitation, for me its just the way the language is defined.

ANSI-C does not support pointer arithmetic for function pointers, more concretely ANSI-C defines pointer arithmetic for object type pointers only, here the definition for the addition:

6.3.6  Additive operators
....
      [#2]  For  addition,  either  both   operands   shall   have
       arithmetic  type,  or  one  operand shall be a pointer to an
       object  type  and  the  other  shall  have   integer   type.
       (Incrementing is equivalent to adding 1.)

 And object types are distinct types form function types:

6.1.2.5 Types...       Types  are  partitioned       into  object  types  (types that describe objects), function       types (types that describe functions), and incomplete  types       (types  that describe objects but lack information needed to       determine their sizes).

 So in other words, ANSI-C does not support pointer arithmetic on function pointers.

Daniel

PS: Snippets above copied from some C standard draft found on the web.




Message Edited by CompilerGuru on 2008-11-12 03:24 PM
0 项奖励
回复