![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
KPL Guest
|
Function pointer in structs |
Posted: Thu Nov 11, 2004 8:28 am |
|
|
Ehm, I cant seem to make either of these two possibilites work (ver. 3.206):
Code: |
typedef void (*resp_func_ptr)(char* a); /*!< Signature for all response functions */
typedef struct
{
char cmd[11];
resp_func_ptr resp_func;
int8 parameter;
}COMMANDSET; /*!< Common definition for module command sets */
|
This compiles, but I get the following error message when I try to call the function:
Expecting function name
I try calling it like this:
Code: |
(*(tmp_cmd_ptr+location[1])->resp_func)(str);
|
(Ive tried several other attempts, include a local function pointer value, and copying the value of the struct f.ptr into that, to no use.
My other go looked like this:
Code: |
typedef struct
{
char cmd[11];
void (*resp_func_ptr)(char* a);
int8 parameter;
}COMMANDSET; /*!< Common definition for module command sets */
|
But this now I get an error message saying 'Unknown type' in the line with the function pointer definition.
Does anyone know what might be the problem? |
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Nov 11, 2004 8:49 am |
|
|
Here's something I came up with for another poster
Code: | /*****************************************************************************
This is an example of how to create a const array of function
pointers and call them. It is a bit of a trick to fool the compiler
but seems to work for the cases that I tested.
******************************************************************************/
#if defined(__PCM__)
#include <16f877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#elif defined(__PCH__)
#include <18f452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#endif
// Here is where we define the start of menu wrappers.
// !!!! Note that it (all wrapper addresses) must be within
// 1024 bytes of the caller
#define MENU_START_ADDRESS 0x200
// This is how many bytes the wrappers take up. You might have to
// set this value high and then determine how many the wrappers actually
// take up. Note that a BRA is usually used for small programs. However
// on a larger program a CALL instruction might be used which will
// require 2 additional bytes
#define FUNC_SIZE 8
// This is where we define our menu wrapper locations
#define MENU1_ADDRESS (MENU_START_ADDRESS)
#define MENU2_ADDRESS (MENU1_ADDRESS+FUNC_SIZE)
#define MENU3_ADDRESS (MENU2_ADDRESS+FUNC_SIZE)
#define MENU4_ADDRESS (MENU3_ADDRESS+FUNC_SIZE)
// Typedef for our function pointer
typedef int (*pFunc)(void);
// Typedef for our menu items
typedef struct {
char name[16];
int16 ptrFunc; // Just tricking the compiler here
} MenuItem;
// Here's that constant array for the menus
const MenuItem Menu[3]={{"String1", MENU1_ADDRESS},
{"String2", MENU2_ADDRESS},
{"String3", MENU3_ADDRESS}};
// These are the real Menus that we want to call
#org 0x2000,0x2100 // org'ed this just to show a CALL insteal of BRA
int RealMenuFunc1(void)
{
int i = 1;
return(i);
}
int RealMenuFunc2(void)
{
int i = 2;
return(i);
}
int RealMenuFunc3(void)
{
int i = 3;
return(i);
}
// These are wrapper menus to make setting the addresses easier.
// It also appears CCS is using a RCALL instruction which limits
// how far the menus can be away from the caller.
#org MENU1_ADDRESS, MENU1_ADDRESS + FUNC_SIZE - 1
int WrapperMenu1(void)
{
return(RealMenuFunc1());
}
#org MENU2_ADDRESS, MENU2_ADDRESS + FUNC_SIZE - 1
int WrapperMenu2(void)
{
return(RealMenuFunc2());
}
#org MENU3_ADDRESS, MENU3_ADDRESS + FUNC_SIZE - 1
int WrapperMenu3(void)
{
return(RealMenuFunc3());
}
void main()
{
int i;
pFunc ptrtofunc;
// Dummy initialization to keep the compiler from complaining
ptrtofunc = WrapperMenu1;
// Assign the address to the function pointer
ptrtofunc = (pFunc)Menu[0].ptrFunc;
// Call the function
i=(*ptrtofunc)();
ptrtofunc = (pFunc)Menu[1].ptrFunc;
i=(*ptrtofunc)();
ptrtofunc = (pFunc)Menu[2].ptrFunc;
i=(*ptrtofunc)();
while( TRUE )
{
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
KPL Guest
|
|
Posted: Thu Nov 11, 2004 8:53 am |
|
|
Yeah, Ive been studying that thread a bit just now.
I dont think it would be worth it in this case though. Ill have several struct arrays and if I have to define and map out the location each its gonna take way too much time compared to my non-functionpointer solution.
But thanks though :D
Do you know if the code Trampas posted (right after yours) actually worked? |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Thu Nov 11, 2004 8:59 am |
|
|
hmm, just read something another place..I guess you HAVE to let the compiler know the location of the function...sigh |
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Nov 11, 2004 9:00 am |
|
|
No, that is a niffty way of doing command parsers. He wanted to be able to do that so I came up with a way to do it by declaring function pointers. If you don't want to use the pointers, just put a const in there and then use a switch statement to call the proper function. |
|
![](templates/subSilver/images/spacer.gif) |
KPL Guest
|
|
Posted: Thu Nov 11, 2004 3:46 pm |
|
|
well, thats what Im doing now. Im gonna try your take on it tonight. Once again thanks!
Though this is gonna require a bit more work, and defeat my original intent of writing a system core that would enable modules to be developed rather independantly. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|