|
|
View previous topic :: View next topic |
Author |
Message |
mradrenalin
Joined: 15 Jun 2009 Posts: 2
|
Array of function pointer |
Posted: Mon Jun 15, 2009 2:39 am |
|
|
Hi all,
i'm using 4.057 version of compiler. Also, I am not so experienced on C. I have PIC18F4550 and i want to write a array of pointer. I have searched enough array of function pointers in C and i couldn't find working example with CCS compiler. is there any different or specific way to write array of function pointer in CCS? Basically what i need is to call functions with just numbers. So i thought the best way is using array of function pointers but i doesn't work. I tried dozens of examples for instance code below is not compiling. it gives an error to these expressions. Could you help me about writing proper way of array of function pointer in CCS.. thank you
Error at
int (*p[4]) (int x, int y);
and
p[0] = sum;
Code: |
#include <stdio.h>
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);
int main(void)
{
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() */
printf("Enter two numbers: ");
scanf("%d %d", &i, &j);
printf("0: Add, 1: Subtract, 2: Multiply, 3: Divide\n");
do {
printf("Enter number of operation: ");
scanf("%d", &op);
} while(op<0 || op>3);
result = (*p[op]) (i, j);
printf("%d", result);
return 0;
}
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;
|
|
|
|
Ttelmah Guest
|
|
Posted: Mon Jun 15, 2009 3:04 am |
|
|
Getting the bracketting 'right', is absolutely critical on function pointer declarations. The original K&R examples will work, but layouts that are fine in other C's, give problems in CCS.
Use typedef, to avoid problems.
So:
Code: |
typedef int (*intfptr) (int a,int b);
intfptr p[4];
|
As another 'comment', the real weakness, with the CCS implementation, is that the last time I tried, it wouldn't allow the array to be initialised at compile time, requiring it to be 'manually' initalised, as you do. A pain....
Best Wishes |
|
|
Guest
|
|
Posted: Mon Jun 15, 2009 7:11 am |
|
|
Ttelmah wrote: | Getting the bracketting 'right', is absolutely critical on function pointer declarations. The original K&R examples will work, but layouts that are fine in other C's, give problems in CCS.
Use typedef, to avoid problems.
So:
Code: |
typedef int (*intfptr) (int a,int b);
intfptr p[4];
|
As another 'comment', the real weakness, with the CCS implementation, is that the last time I tried, it wouldn't allow the array to be initialised at compile time, requiring it to be 'manually' initalised, as you do. A pain....
Best Wishes |
Thanks for your help. typedef works for defining function pointer but i couldn't make array from it.
doesn't work.
also i tried different version like
Code: |
int (*intfptr[2])(int x)={NULL}
|
it doesn't work either. compiler is expected numeric expression after int |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Mon Jun 15, 2009 10:00 am |
|
|
Ttelmah wrote: | Getting the bracketting 'right', is absolutely critical on As another 'comment', the real weakness, with the CCS implementation, is that the last time I tried, it wouldn't allow the array to be initialised at compile time, requiring it to be 'manually' initalised, as you do. A pain....
Best Wishes |
But that is not limited to the CCS implementation right? I think the HT-Soft compiler has the same problem. |
|
|
|
|
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
|