|
|
View previous topic :: View next topic |
Author |
Message |
pictrance
Joined: 18 Jul 2010 Posts: 14 Location: Puebla, Mexico
|
Error pointer function in a struct |
Posted: Sun Apr 19, 2020 2:55 pm |
|
|
Hello everyone.
I am making an FSM state machine, using structures and functions. I using CCS 5.093
I am declaring a pointer function in a struct:
Code: | struct state {
void (*funcionPt) (void); // Function pointer
unsigned int16 time;
unsigned char next[4];
}; |
to be able to use it in an array and thus be able to call various functions:
Code: |
Styp FSM [4] = {
{&Advance_N, 3000, {goN, waitN, goN, waitN}},
{&Wait_N, 0, {goE, goE, goE, goE}},
{&Avance_E, 3000, {goE, goE, waitE, waitE}},
{&Wait_E, 0, {goN, goN, goN, goN}}
};
|
but I get the following list of errors:
Code: |
Compiling D: \ Structure_State Machine \ Main_Structure_Structure_Functions on Apr 19, 20 at 3:32 PM
*** Error 27 "D: \ Structure_State Machine \ Main_Structure_State_Functions.c" Line 119 (23,24): Expression must evaluate to a constant
*** Error 43 "D: \ Structure_State Machine \ Main_Structure_Structure_Functions.c" Line 119 (24,28): Expecting a declaration
.
.
.more same errors
|
and for this line of code inside the main where I call the function:
Code: | (FSM[actual].funcionPt) (); |
I get the following error:
Code: | *** Error 56 "D: \ Structure_State Machine \ Main_Structure_State_Functions.c" Line 143 (17,26): Element is not a member |
code works fine on XC8 even works on DevC
Does anyone know how to correct mistakes?
Or is this another C implementation bug in CCS?
the complete code is:
Code: | #include <18F4550.h>
#device ADC=10
#fuses NOMCLR,HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#fuses NOBROWNOUT//,BORV45
#use delay(clock=48M)
void Avance_N(void);
void Espera_N(void);
void Avance_E(void);
void Espera_E(void);
struct state{
void(*funcionPt)(void); //Puntero de funcion
unsigned int time;
unsigned char next[4];
};
typedef struct state Styp;
#define goN 0
#define waitN 1
#define goE 2
#define waitE 3
Styp FSM[4]={
{&Avance_N,3000,{goN,waitN,goN,waitN}},
{&Espera_N,0,{goE,goE,goE,goE}},
{&Avance_E,3000,{goE,goE,waitE,waitE}},
{&Espera_E,0,{goN,goN,goN,goN}}
};
char actual, inpt;
void main() {
actual = 0;
(FSM[actual].funcionPt)();
while(true){
}
}
void Avance_N(void){}
void Espera_N(void){}
void Avance_E(void){}
void Espera_E(void){}
|
_________________ Si el proyecto tiene mal olor es de Química, Si echa humo negro es de Mecánica, Si es verde o se retuerce es de Bioingenieria Y si no funciona es de Electrónica.. :p ... no es mia la frase, pero me gusto.
Last edited by pictrance on Sun Apr 19, 2020 5:41 pm; edited 2 times in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Apr 19, 2020 3:10 pm |
|
|
Your code snippets are different in some places, compared to the entire
program.
Please re-post the entire program with the code snippets that don't work.
Give us the bad program as a whole. Don't post the bad code as snippets.
Also post your CCS compiler version. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1354
|
|
Posted: Sun Apr 19, 2020 3:25 pm |
|
|
In order to do initialization of a function pointer, CCS requires a known type and your struct declaration technically uses an "anonymous" function pointer type. Try starting out with a typedef of the function pointer type:
Code: |
typedef void(*functionPtr)(void);
|
And adjust your struct declaration to
Code: |
struct state{
functionPtr function; //Puntero de funcion
unsigned int time;
unsigned char next[4];
};
|
and your call to:
Code: |
(FSM[actual].function)();
|
Note this might be a bug in the compiler, but I don't know for sure. I don't recall if standard C requires an explicit function pointer type for initialization or not. You should email CCS support about it to see if they can either fix it or explain why it is that way.
Side note: Function pointers take an (relatively) exceptionally long amount of time to dereference and execute on a PIC. Since they reside on separate memory architectures, the compiler has to generate special functions to convert your RAM pointer into a call to a function in ROM. Make sure you are not trying to do time sensitive things with function pointers without benchmarking them.
EDIT: Here is the modified version of your example with a known type for your function pointer. It compiles on 5.093
Code: |
#include <18F4550.h>
#device ADC=10
#fuses NOMCLR,HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#fuses NOBROWNOUT//,BORV45
#use delay(clock=48M)
void Avance_N(void);
void Espera_N(void);
void Avance_E(void);
void Espera_E(void);
typedef void(*functionPtr)(void);
struct state{
functionPtr function; //Puntero de funcion
unsigned int time;
unsigned char next[4];
};
typedef struct state Styp;
#define goN 0
#define waitN 1
#define goE 2
#define waitE 3
Styp FSM[4]={
{&Avance_N,3000,{goN,waitN,goN,waitN}},
{&Espera_N,0,{goE,goE,goE,goE}},
{&Avance_E,3000,{goE,goE,waitE,waitE}},
{&Espera_E,0,{goN,goN,goN,goN}}
};
char actual, inpt;
void main() {
actual = 0;
(FSM[actual].function)();
while(true){
}
}
void Avance_N(void){}
void Espera_N(void){}
void Avance_E(void){}
void Espera_E(void){}
|
|
|
|
pictrance
Joined: 18 Jul 2010 Posts: 14 Location: Puebla, Mexico
|
|
Posted: Sun Apr 19, 2020 5:43 pm |
|
|
PCM programmer wrote: | Your code snippets are different in some places, compared to the entire
program.
Please re-post the entire program with the code snippets that don't work.
Give us the bad program as a whole. Don't post the bad code as snippets.
Also post your CCS compiler version. |
oh sorry it was for using google translate
and I'm using CCS 5,093 _________________ Si el proyecto tiene mal olor es de Química, Si echa humo negro es de Mecánica, Si es verde o se retuerce es de Bioingenieria Y si no funciona es de Electrónica.. :p ... no es mia la frase, pero me gusto. |
|
|
pictrance
Joined: 18 Jul 2010 Posts: 14 Location: Puebla, Mexico
|
|
Posted: Sun Apr 19, 2020 6:15 pm |
|
|
jeremiah wrote: | In order to do initialization of a function pointer, CCS requires a known type and your struct declaration technically uses an "anonymous" function pointer type. Try starting out with a typedef of the function pointer type:
Code: |
typedef void(*functionPtr)(void);
|
And adjust your struct declaration to
Code: |
struct state{
functionPtr function; //Puntero de funcion
unsigned int time;
unsigned char next[4];
};
|
and your call to:
Code: |
(FSM[actual].function)();
|
Note this might be a bug in the compiler, but I don't know for sure. I don't recall if standard C requires an explicit function pointer type for initialization or not. You should email CCS support about it to see if they can either fix it or explain why it is that way.
Side note: Function pointers take an (relatively) exceptionally long amount of time to dereference and execute on a PIC. Since they reside on separate memory architectures, the compiler has to generate special functions to convert your RAM pointer into a call to a function in ROM. Make sure you are not trying to do time sensitive things with function pointers without benchmarking them.
EDIT: Here is the modified version of your example with a known type for your function pointer. It compiles on 5.093....... |
wow
excellent, now it works
Regards _________________ Si el proyecto tiene mal olor es de Química, Si echa humo negro es de Mecánica, Si es verde o se retuerce es de Bioingenieria Y si no funciona es de Electrónica.. :p ... no es mia la frase, pero me gusto. |
|
|
|
|
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
|