View previous topic :: View next topic |
Author |
Message |
rougie
Joined: 14 Jun 2006 Posts: 31
|
Code problem with function pointers ! |
Posted: Fri Jan 23, 2009 11:49 pm |
|
|
Can someone point out why I am getting an error in this simple code fragment?
Code: |
#include <Dv4685.h> // DEVICE INNITIALIZATIONS
#include <stdlib.h> // CCS LIBRARY STANDARD LIBRARY
int sum(int a, int b);
int subtract(int a, int b);
int mul(int a, int b);
int div(int a, int b);
int (*p[4])(int x, int y);
void main()
{
int result;
int i, j, op;
p[0] = sum; /* address of sum() */
p[1] = subtract; /* address of subtract() */
p[2] = mul; /* address of mul() */
p[3] = div; /* address of div() */
op = 1;
result = (*p[op]) (i, j);
}
int sum(int a, int b)
{
return a + b;
}
int subtract(int a, int b)
{
return a - b;
}
int mul(int a, int b)
{
return a * b;
}
int div(int a, int b)
{
if(b)
return a / b;
else
return 0;
} |
All help appreciated!
Rob |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jan 24, 2009 12:18 am |
|
|
Always post:
1. Your PIC.
2. The compiler version.
3. The error message.
4. Indicate the line of code in your program that has the error. |
|
|
MicroManiac
Joined: 21 Aug 2008 Posts: 34
|
Re: Code problem! |
Posted: Sat Jan 24, 2009 12:28 am |
|
|
rougie wrote: |
int (*p[4])(int x, int y);
|
Well first of all, I'm not sure that this is the right way to return an array in a function call
you should try:
Code: | int *p(int x, int y); |
Second
this is not the way to call a function.
in the function prototype, you specified that you need parameters
this can cause the error. try
_________________ "Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction."
Albert Einstein |
|
|
rougie
Joined: 14 Jun 2006 Posts: 31
|
|
|
rougie
Joined: 14 Jun 2006 Posts: 31
|
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sat Jan 24, 2009 10:05 am |
|
|
See CCS C manual:
Quote: | The compiler does not permit pointers to functions. |
Best regards,
Frank |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jan 24, 2009 1:10 pm |
|
|
That note is probably not up date. Function pointers do work at least
on 18F, but they require a late version, and sometimes require a work-
around. Example:
http://www.ccsinfo.com/forum/viewtopic.php?t=33458
I don't guarantee that they can be made to work in an array of
function pointers. It would require testing. |
|
|
rougie
Joined: 14 Jun 2006 Posts: 31
|
|
Posted: Sat Jan 24, 2009 4:14 pm |
|
|
>I don't guarantee that they can be made to work in an array of
>function pointers. It would require testing.
Disappointing!
Robert |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sat Jan 24, 2009 6:13 pm |
|
|
Quote: | That note is probably not up date. Function pointers do work at least on 18F, but they require a late version, and sometimes require a work-around. |
Yes, surprisingly. Unfortunately, no CCS doc mentions it. Readme.txt, said to contain the latest updates to the compiler hasn't a word on it. So there is an interesting question: Does the compiler manual and supplementing dóc mean anything?
I found, that array of function pointers seems to be supported rather straightforward. I didn't check code operation, but it looks meaningful from the assembly listing.
Code: | #include <18F452.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
void funct1(int8 par1)
{
printf("Hello Funct1 %i\n\r",par1);
}
void funct2(int8 par1)
{
printf("Hello Funct2 %i\n\r",par1);
}
// Typedef a function pointer.
typedef void (*_fptr)(int8);
void main()
{
int8 i;
_fptr fptr[2] = {funct1,funct2};
for (i=0;i<2;i++)
(*fptr[i])(10+i);
while(1);
} |
|
|
|
rougie
Joined: 14 Jun 2006 Posts: 31
|
|
Posted: Sun Jan 25, 2009 10:39 am |
|
|
Thanks FvM!
Your sample code regarding arrays of function pointers works!
I am using PCWH version : 4.083
Thanks again!
Roberto |
|
|
|