CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Function Pointer fails when defined outside of structures

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Pets



Joined: 06 May 2019
Posts: 6

View user's profile Send private message

Function Pointer fails when defined outside of structures
PostPosted: Mon May 06, 2019 11:35 am     Reply with quote

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: 19538

View user's profile Send private message

PostPosted: Mon May 06, 2019 2:02 pm     Reply with quote

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. Smile
Pets



Joined: 06 May 2019
Posts: 6

View user's profile Send private message

PostPosted: Mon May 06, 2019 2:28 pm     Reply with quote

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: 19538

View user's profile Send private message

PostPosted: Mon May 06, 2019 11:50 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu May 09, 2019 7:44 am     Reply with quote

will do... better safe than sorry!
Thanks for all your help and advice...
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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