![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
JH1987
Joined: 22 Jan 2011 Posts: 11
|
Function Pointer as Function Argument Syntax |
Posted: Tue Mar 08, 2011 2:32 pm |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19358
|
|
Posted: Tue Mar 08, 2011 4:08 pm |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
JH1987
Joined: 22 Jan 2011 Posts: 11
|
Thanks |
Posted: Tue Mar 08, 2011 5:15 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19358
|
|
Posted: Wed Mar 09, 2011 3:06 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
JH1987
Joined: 22 Jan 2011 Posts: 11
|
Good info |
Posted: Wed Mar 09, 2011 5:57 pm |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|