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

Code problem with function pointers !

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



Joined: 14 Jun 2006
Posts: 31

View user's profile Send private message

Code problem with function pointers !
PostPosted: Fri Jan 23, 2009 11:49 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Jan 24, 2009 12:18 am     Reply with quote

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

View user's profile Send private message Send e-mail MSN Messenger

Re: Code problem!
PostPosted: Sat Jan 24, 2009 12:28 am     Reply with quote

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
Quote:
p[0] = sum

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
Code:
p[0] = sum(i,j);

_________________
"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

View user's profile Send private message

PostPosted: Sat Jan 24, 2009 8:58 am     Reply with quote

>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:


http://www.java2s.com/Code/C/Function/Initializethefunctionpointerarray.htm

???

standing by!
rougie



Joined: 14 Jun 2006
Posts: 31

View user's profile Send private message

PostPosted: Sat Jan 24, 2009 9:04 am     Reply with quote

ooops!

I meant:

http://www.java2s.com/Code/C/Function/ArraysofPointerstofunctions.htm

But I see that it is declared in main() !!!!

Let me try something!

I'll get back!
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat Jan 24, 2009 10:05 am     Reply with quote

See CCS C manual:
Quote:
The compiler does not permit pointers to functions.

Best regards,
Frank
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jan 24, 2009 1:10 pm     Reply with 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. 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

View user's profile Send private message

PostPosted: Sat Jan 24, 2009 4:14 pm     Reply with quote

>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

View user's profile Send private message

PostPosted: Sat Jan 24, 2009 6:13 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Jan 25, 2009 10:39 am     Reply with quote

Thanks FvM!

Your sample code regarding arrays of function pointers works!

I am using PCWH version : 4.083

Thanks again!

Roberto
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