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 support@ccsinfo.com

Function Pointer as Function Argument Syntax

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



Joined: 22 Jan 2011
Posts: 11

View user's profile Send private message

Function Pointer as Function Argument Syntax
PostPosted: Tue Mar 08, 2011 2:32 pm     Reply with quote

Hey guys,

Simple question. I want to use a fn as an argument in another fn. So of course I'm using a function pointer. I looked up the C syntax just to make sure I wasn't crazy, and it matches what I'm doing. Strangely, the current CCS manual says that they don't support function pointers, but I see from this forum that people are using them. However, I don't see anybody passing them as arguments to functions. Is this supported in CCS? If so, is my syntax incorrect?

Here's my test program, which fails to compile with the error

Quote:
*** Error 29 "fn_ptr_test.c" Line 13(33,34): Function definition different from previous definition


Code:


#include <18F4520.h>
#device PASS_STRINGS = IN_RAM
#include "fuses.h"

#use delay(INTERNAL=8M, clock=32M)

#use rs232(BAUD=9600, XMIT=Pin_C6, BITS=8, PARITY=N)

void my_fn(int n) {
   printf("Arg is %u\r\n",n);
}

void fn_that_takes_fn_args(void (*fn)(int)) {
   (*fn)();
}

void main() {    
   setup_oscillator(OSC_32MHZ);
   delay_ms(10);               //no put

   printf("Fn Ptr Test\r\n");
   delay_ms(1000);
   fn_that_takes_fn_args(my_fn(0));
   delay_ms(1000);
   fn_that_takes_fn_args(my_fn(1));
   delay_ms(1000);
   while(1);
}



Thanks very much,

JH
Ttelmah



Joined: 11 Mar 2010
Posts: 19358

View user's profile Send private message

PostPosted: Tue Mar 08, 2011 4:08 pm     Reply with quote

They documented it in the 'readme', several versions ago, and use it in one example, and in qsort.
The syntax is remarkably 'fussy'. Easiest way to get it to work, is to copy their qsort syntax. So:

Code:

typedef void (*_fnptr)(int n);

void my_fn(int n) {
   printf("Arg is %u\r\n",n);
}

void fn_that_takes_fn_args(_fnptr fn, int nx) {
   (*fn)(nx);
}


//Then call it with:
   fn_that_takes_fn_args(my_fn,1);

If you try to type exactly the same syntax, 'inline', it never seems to work.

Best Wishes
JH1987



Joined: 22 Jan 2011
Posts: 11

View user's profile Send private message

Thanks
PostPosted: Tue Mar 08, 2011 5:15 pm     Reply with quote

Thanks for the clarification. Your syntax works perfectly. I was confused by reading
Quote:
The compiler does not permit pointers to functions so that the compiler can know at compile time the complete call tree.
in the manual FAQ.
Ttelmah



Joined: 11 Mar 2010
Posts: 19358

View user's profile Send private message

PostPosted: Wed Mar 09, 2011 3:06 am     Reply with quote

Many parts of the manual are 'residual'. Left over from older versions. This applies also to pointers to constants, where the later compilers have the 'ROM', as opposed to const directive, that does allow pointers to be built, but at a cost in size, and also the use of 'PASS_STRINGS_IN_ROM', allows const strings to be used in a manner more akin to RAM based strings.
This is where the forum really is a great resource, allowing these 'holes' in the manual to be plugged. :-)

Best Wishes
JH1987



Joined: 22 Jan 2011
Posts: 11

View user's profile Send private message

Good info
PostPosted: Wed Mar 09, 2011 5:57 pm     Reply with quote

I am very impressed with the CCS forum. Its an excellent resource, especially if you're not a full-time PIC programmer but have to keep going back to it. I wasn't aware of the "ROM" change, but I'll read about it. Thanks for the info and your help.

JH
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