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

Kinda "pointer to function" question!

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



Joined: 30 Oct 2003
Posts: 13

View user's profile Send private message

Kinda "pointer to function" question!
PostPosted: Tue Sep 14, 2004 7:50 am     Reply with quote

Heyhey!

This code works:

Code:

#device *=16

byte Read(myData index) {
    return *index;
}

void main() {
    typedef byte *myData;
    typedef byte (*readFunc)(myData index);
    readFunc  do_Read;

    do_Read = Read;
}



But this one doesn't:

Code:

#device *=16

byte Read(myData index) {
    return *index;
}

void main() {
    typedef byte *myData;
    typedef int16 (*readFunc)(myData index);
    readFunc  do_Read;

    do_Read = Read;     // (*)
}


It returns with:

CCS 3.184 wrote:

*** Error 118 "...\test.c" Line (*): Invalid type conversion


What am I missing?

Markus
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Tue Sep 14, 2004 10:27 am     Reply with quote

Code:

byte Read(myData index)


The function is declared with a return value of a byte and not int16
Markus



Joined: 30 Oct 2003
Posts: 13

View user's profile Send private message

PostPosted: Tue Sep 14, 2004 11:35 am     Reply with quote

Mark, you're right. I made this mistake while writing my first post. It _is_ defined as int16 in the project, but that doesn't work (see above)...

Markus
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Tue Sep 14, 2004 11:50 am     Reply with quote

Not sure what you are trying to do but this works just fine:

Code:

#if defined(__PCM__)
#include <16f877.h>
#device *=16
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
 
#elif defined(__PCH__)
#include <18f452.h>
#device *=16
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#endif

typedef byte *myData;
typedef int16 (*readFunc)(myData index);

int16 Read(myData index)
{
  return *index;
}

void main()
{
  readFunc  do_Read;
  mydata test;
  byte input = 123;
  int16 result;

  test = &input;
  do_Read = Read;     
  result = (*do_Read)(test);
  while(1);
}
Tom-H-PIC



Joined: 08 Sep 2003
Posts: 105
Location: New Castle, DE

View user's profile Send private message

pointers to function ?
PostPosted: Tue Sep 14, 2004 1:20 pm     Reply with quote

Ok the question do pointers work with function or not?

Maybe I have not totally thought this through but what is the point of the pointer to a function thing?
If a person writes a function and uses it in code the compiler uses a goto in the list file to jump to the function. To me this would be the same as a pointer to the function.

Or are we saying that we would be able to use pointers to a function like a pointer to fprintf and only have one fprintf in the total code.

Thanks guys it will help if you can help clear up my thinking on this.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Tue Sep 14, 2004 1:30 pm     Reply with quote

With a pointer to a function, you can dynamically assign a function call to a variable and then call it. It is great for doing menus and command parsers. You only need one handler and typically get the function addresses from a constant look up table. Here is another use. Let say you have a device that has 2 ports. Lets say you want to be able to select which port the data goes out at runtime. Well you just create 2 functions. One for each port. Now all your output uses the function pointer. Simply set the pointer equal to the function that you want to use (maybe something like
Code:

if (debug)
  pFunc = DebugOutput;
else
  pFunc = NormalOutput;

) does it make sense yet?

More reading: http://www.ccsinfo.com/forum/viewtopic.php?t=20416
Trampas



Joined: 04 Sep 2004
Posts: 89
Location: NC

View user's profile Send private message MSN Messenger

PostPosted: Tue Sep 14, 2004 7:03 pm     Reply with quote

Another example of using pointer to function is in state machines. Do you guys remeber Melly and Moore (spelling?) state machines? Well you can set up a table of states and what function to call when you are in that state and/or what function to call when you change states.

Trampas
Bilk
Guest







pointers to functions
PostPosted: Thu Sep 16, 2004 1:21 pm     Reply with quote

Beware: I have found that pointers to functions work -- but there seems to be a problem if an interrupt occurs exactly when the pointer to function call is being pushed.

The ISR_() destroys the stack push and the program continues neatly. The desired (*ptr_funct)() is then never called.

Workaround, disable interrupts immediately prior to making (*ptr_funct)(); call and renable as first item in called functions.

Compiler version 3.208 - PIC18f452
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