|
|
View previous topic :: View next topic |
Author |
Message |
Micka
Joined: 10 Jun 2007 Posts: 5
|
struct of function's pointer |
Posted: Sat Jan 26, 2008 2:50 pm |
|
|
How to make a structure with function pointers like this :
struct test{
void (*tst)(int,char);
void (*tst2)(void*,char);
};
I have this error : Unknown type
Thanks for help |
|
|
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 |
|
|
Micka
Joined: 10 Jun 2007 Posts: 5
|
|
Posted: Sun Jan 27, 2008 11:58 am |
|
|
Thank you for your help. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|