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

Problem with definition of function

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



Joined: 15 Oct 2011
Posts: 36

View user's profile Send private message

Problem with definition of function
PostPosted: Tue Jun 19, 2012 1:54 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jun 20, 2012 6:32 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jun 21, 2012 1:30 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jun 21, 2012 2:31 am     Reply with quote

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

Best Wishes
Lemosek



Joined: 15 Oct 2011
Posts: 36

View user's profile Send private message

PostPosted: Thu Jun 21, 2012 9:49 am     Reply with quote

Hello,
dear Ttelmah thank You for Your answer and help Smile now is ok.

Best regards
R.L.
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