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 CCS Technical Support

struct of function's pointer

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



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jan 26, 2008 10:56 pm     Reply with quote

The program below demonstrates using a function pointer in a structure.
I tested this with PCH vs. 4.066 and it works. It displays the following:
Quote:
Hello World


You will notice that I have to move the function pointer from the
structure element into an intermediate variable ('temp') before I call it.
I couldn't find a way to do it without using the intermediate variable.
I don't have any more time to spend on it.
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)

// Create a simple function that displays some text.
void Hello(void)
{
printf("Hello World\n\r");
}

// Typedef a function pointer.
typedef void (*_fptr)(void);


// Create a structure that contains two variables
// and a fuction pointer.  Initialize them.
struct
{
int8 value;         
int8 temp;             
_fptr func_ptr;
}My_struct = {0, 0, Hello};

   
//====================================
void main()
{
// Create a temp variable to hold a function pointer.
_fptr temp;   

// Copy the function pointer from the structure into 'temp'.
temp = My_struct.func_ptr;

// Call the Hello function.
(*temp)();

while(1);     
}


CCS has this on their versions page:
Quote:
4.058 A bug involving pointers to functions is fixed

If you have an earlier version there might be problems.
In that case, see this thread. It might help:
http://www.ccsinfo.com/forum/viewtopic.php?t=31637
Micka



Joined: 10 Jun 2007
Posts: 5

View user's profile Send private message

PostPosted: Sun Jan 27, 2008 11:58 am     Reply with quote

Thank you for your help.
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