View previous topic :: View next topic |
Author |
Message |
matrixofdynamism
Joined: 06 Dec 2010 Posts: 25
|
Are pointers to functions still not supported in CCS PIC C? |
Posted: Sun Jul 03, 2016 1:22 pm |
|
|
I have written some code that uses a function pointer, it is assigned a value in a C language switch block and the function pointed to is called after the switch block.
The code does not compile. After thinking about it for some time I have a feeling that pointers to functions are not supported by the PIC C Compiler. Is this correct? I am using a PIC 18F device. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jul 03, 2016 1:35 pm |
|
|
Yes, they normally work. Post your small but complete test program
and your compiler version. |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
Re: Are pointers to functions still not supported in CCS PIC |
Posted: Mon Jul 04, 2016 1:54 am |
|
|
matrixofdynamism wrote: | The code does not compile. |
What error messages do you get?
Quote: | After thinking about it for some time I have a feeling that pointers to functions are not supported by the PIC C Compiler. Is this correct? |
No, it is not correct, function pointers are supported, but there restrictions. Basically, the compiler has to be able to resolve the pointer. Assignment at runtime, i.e. in your switch block, is fine provided that compiler can work out where the function is. As we saw recently in another thread, built-in functions, i.e. those implemented by the compiler, are often not functions in the C sense and cannot be pointed to.
Generally speaking, its often better to avoid function pointers in CCS C. Its certainly easier. and more maintainable (e.g. is more likely to work in future complier versions). This is due to the implications of the Harvard architecuture of most PICS. In your case, do you really need to assign a pointer in a switch and then call it afterwards? It might require reorganisation, but can you not simply call the relevant routine directly in the switch block? |
|
|
|