View previous topic :: View next topic |
Author |
Message |
Pets
Joined: 06 May 2019 Posts: 6
|
Function Pointer fails when defined outside of structures |
Posted: Mon May 06, 2019 11:35 am |
|
|
The function pointer defined in the structure works fine.
The 'TestSpeak' function pointer only works when declared in a local function not as a global.
Any thoughts???
/* **********************************************************
Configure
********************************************************** */
// CPU
#include <18F27K42.h>
// PPS Module
#pin_select U1RX=PIN_C7
#pin_select U1TX=PIN_C6
// Misc
#fuses RSTOSC_HFINTRC_64MHZ,NOWDT,PROTECT,NOMCLR
#use delay(clock=64000000)
#use rs232(UART1, STREAM=UART_1, BAUD=115000, XMIT=PIN_C6, RCV=PIN_C7, ERRORS, DISABLE_INTS)
/* **********************************************************
Define
********************************************************** */
void ProcessDog(void* aCounter);
void ProcessCat(void* aCounter);
void ProcessBird(void* aCounter);
typedef void(*funcPtr)(void*);
typedef struct
{
funcPtr Speak; // This function Pointer compiles and works fine
}ST_ANIMAL;
/* **********************************************************
Declare
********************************************************** */
ST_ANIMAL stAnimals[3];
//funcPtr TestSpeak; // If declared here TestSpeak compiles with error shown on line 80
void ProcessDog(void* aCounter)
{
fprintf(UART_1, "Woof %LU \r\n", (int16)aCounter[0]);
}
void ProcessCat(void* aCounter)
{
fprintf(UART_1, "Meow %LU \r\n", (int16)aCounter[0]);
}
void ProcessBird(void* aCounter)
{
fprintf(UART_1, "Tweet %LU \r\n", (int16)aCounter[0]);
}
funcPtr TestSpeak; // If declared here TestSpeak compiles with error shown on line 80
void main()
{
// Data
int idx;
int16 counter;
//funcPtr TestSpeak; // If declared here TestSpeak compiles and works fine
// Init
counter = 0;
TestSpeak = &ProcessDog;
stAnimals[0].Speak = &ProcessDog;
stAnimals[1].Speak = &ProcessCat;
stAnimals[2].Speak = &ProcessBird;
// Loop!
fprintf(UART_1, "Test Function Pointers \r\n\n");
while(1)
{
TestSpeak(&counter); // compile error shows on this line 'Function used but not defined'
for(idx = 0 ; idx < 3 ; idx++)
{
stAnimals[idx].Speak(&counter); // This call works fine
}
counter++;
fprintf(UART_1, "\n");
delay_ms(2000);
}
} |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Mon May 06, 2019 2:02 pm |
|
|
It's interesting that it accepts it in the array. However the reason it doesn't
work is the syntax being used to call the function. The correct C syntax to
call a function defined by a pointer is:
(*TestSpeak)(&counter);
Try it. |
|
|
Pets
Joined: 06 May 2019 Posts: 6
|
|
Posted: Mon May 06, 2019 2:28 pm |
|
|
Thanks Ttelmah
Yes it did work!!!
Interesting I do not have to do that if I declare TestSpeak in main(). |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Mon May 06, 2019 11:50 pm |
|
|
It is slightly 'interesting'. The problem is that calling without the
dereferencing, is asking the code to call a function, not telling it to
call a variable containing a function address. Now the compiler seems
to be automatically dereferencing when the value is in the array or
declared locally. This is 'non documented' behaviour, so I'd prefer to
allways explicitly dereference. This way you won't get unexpected
results!... |
|
|
Pets
Joined: 06 May 2019 Posts: 6
|
|
Posted: Thu May 09, 2019 7:44 am |
|
|
will do... better safe than sorry!
Thanks for all your help and advice... |
|
|
|