View previous topic :: View next topic |
Author |
Message |
Lemosek
Joined: 15 Oct 2011 Posts: 36
|
Problem with definition of function |
Posted: Tue Jun 19, 2012 1:54 pm |
|
|
Hello,
I write simple test code but I have error:
Function definition different from previous definition
I use similar code at gcc compiler and it works.
What I'm doing wrong at CCS ??
Best regards
R.L.
Code: |
#include <18F2580.h>
#fuses HS, NOPROTECT, PUT, BROWNOUT, NOWDT, NOLVP
#use delay(clock=20M)
//****************************************************************
// wskaźnik do funkcji callback dla zdarzenia EVENT()
static void (*event_callback)(char *pBuf);
// funkcja do rejestracji funkcji zwrotnej w zdarzeniu EVENT()
void register_event_callback(void(*callback)(char *pBuf)) {
event_callback = callback;
}
// Zdarzenie
void EVENT(char * rbuf) {
if( flag ) { //if flag is active
if( event_callback ) {
(*event_callback)( rbuf );
}
}
}
void parse_uart_data( char * pBuf ) {
//jakis kod
}
char bufor[100];
int1 flag=0;
//======================================
void main()
{
delay_ms(1000);
// rejestracja własnej funkcji
register_event_callback( parse_uart_data );
While(1){
EVENT(bufor); // zdarzenie
}
}
|
|
|
|
khalis
Joined: 12 Feb 2009 Posts: 54
|
|
Posted: Wed Jun 20, 2012 6:32 pm |
|
|
Hi, i think how your define your pointer is wrong.
Yours:
[EVENT(bufor);]
Should change to:
[EVENT(bufor[n]);]
n = no of your memory address (bufor)[code][] |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Thu Jun 21, 2012 1:30 am |
|
|
CCS, is stricter than most compilers about definition's. Definitions used have to match exactly. This is where things like typedef (which allow you to ensure they do match), come in very useful. So:
Code: |
#include <18F2580.h>
#fuses HS, NOPROTECT, PUT, BROWNOUT, NOWDT, NOLVP
#use delay(clock=20M)
//****************************************************************
typedef void (*event_function)(char *);
// wska?nik do funkcji callback dla zdarzenia EVENT()
event_function event_callback = 0; //must initiliase or it won't test as false
int1 flag=0; //needs to be before use of 'flag' in EVENT
char bufor[100];
// funkcja do rejestracji funkcji zwrotnej w zdarzeniu EVENT()
void register_event_callback(event_function callback) {
event_callback = callback;
}
// Zdarzenie
void EVENT(char * rbuf) {
if( flag ) { //if flag is active
if(event_callback) { //This only guarantees to test 'false' if the defintion is initialised to 0
(*event_callback)( rbuf );
}
}
}
void parse_uart_data( char * pBuf ) {
}
//======================================
void main() {
delay_ms(1000);
// rejestracja w?asnej funkcji
register_event_callback( parse_uart_data );
While(1){
EVENT(bufor); // zdarzenie
}
}
|
No idea if the code that results is right, but it should be.
It does correctly put the address of parse_uart_data into the 'event_callback' variable, and calls it.
Had to also move the 'flag' definition to before where it is used, and make sure that 'event_callback' is initilised to zero, or it isn't guaranteed to test as false.
Best Wishes |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Thu Jun 21, 2012 2:31 am |
|
|
khalis wrote: | Hi, i think how your define your pointer is wrong.
Yours:
[EVENT(bufor);]
Should change to:
[EVENT(bufor[n]);]
n = no of your memory address (bufor)[code][] |
No. The name of an array, is a 'shortcut' for it's address in memory. So 'bufor', is the same as &bufor[0] - the address of the first element in the array. His function want's to receive the address of the array (hence the declaration says it expects a pointer - the '*'), and the function it then calls also expects a pointer (char *). This part is OK.
The problem is with CCS being 'picky' about how you declare a function pointer, and pass this to a function.
Best Wishes |
|
|
Lemosek
Joined: 15 Oct 2011 Posts: 36
|
|
Posted: Thu Jun 21, 2012 9:49 am |
|
|
Hello,
dear Ttelmah thank You for Your answer and help now is ok.
Best regards
R.L. |
|
|
|