PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jan 26, 2008 10:56 pm |
|
|
The program below demonstrates using a function pointer in a structure.
I tested this with PCH vs. 4.066 and it works. It displays the following:
You will notice that I have to move the function pointer from the
structure element into an intermediate variable ('temp') before I call it.
I couldn't find a way to do it without using the intermediate variable.
I don't have any more time to spend on it.
Code: |
#include <18F452.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
// Create a simple function that displays some text.
void Hello(void)
{
printf("Hello World\n\r");
}
// Typedef a function pointer.
typedef void (*_fptr)(void);
// Create a structure that contains two variables
// and a fuction pointer. Initialize them.
struct
{
int8 value;
int8 temp;
_fptr func_ptr;
}My_struct = {0, 0, Hello};
//====================================
void main()
{
// Create a temp variable to hold a function pointer.
_fptr temp;
// Copy the function pointer from the structure into 'temp'.
temp = My_struct.func_ptr;
// Call the Hello function.
(*temp)();
while(1);
} |
CCS has this on their versions page:
Quote: | 4.058 A bug involving pointers to functions is fixed |
If you have an earlier version there might be problems.
In that case, see this thread. It might help:
http://www.ccsinfo.com/forum/viewtopic.php?t=31637 |
|