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

Functions inside structures????

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







Functions inside structures????
PostPosted: Wed Jul 23, 2008 12:50 pm     Reply with quote

I am converting a program written in C18 over to CCS. How can this be done is CCS?

Code:


typedef struct
{
     unsigned int *lk(unsigned int x,unsigned int y);
     unsigned int *dt(unsigned int z,unsigned int r);
}data;



I get the following errors when I compile:
Quote:
Expecting a ; or ,
Expecting an identifier
Expecting a declaration
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jul 23, 2008 1:29 pm     Reply with quote

Here is a demo program that will probably help you. It displays the
following output:
Quote:
Hello World
Hi There


Notice the code in main() copies the function pointer from the
structure variable into a temp variable before calling it.
That's a work-around that I found is necessary in CCS.
I haven't been able to call the function by using the struct.element
representation. It has to be copied into a simple function ptr
variable first. This program was tested with PCH vs. 4.076.
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)

char Hello_str[] = {"Hello "};
char World_str[] = {"World\n\r"};
char Hi_str[] = {"Hi "};
char There_str[] = {"There\n\r"};

// This function returns a pointer to a string.  The desired
// string is selected by using a parameter of 0 or 1.
char *HelloWorld(int8 strnum)
{
if(strnum == 0)
   return(Hello_str);
else
   return(World_str);
}

// This function also returns a pointer to a string.  The
// desired string is selected by using a parameter of 0 or 1.
char *HiThere(int8 strnum)
{
if(strnum == 0)
   return(Hi_str);
else
   return(There_str);
}


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

// Create a structure that contains two variables
// and two fuction pointers.  Initialize them.
struct
{
int8 value;         
int8 my_val;             
_fptr fn1;
_fptr fn2;
}My_struct = {0, 0, HelloWorld, HiThere};  // Init values

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


temp = My_struct.fn1;     // Copy func ptr to temp variable 
string_ptr = (*temp)(0);  // Call the HelloWorld function
printf(string_ptr);       // Display the "Hello" string
string_ptr = (*temp)(1);  // Call the HelloWorld function
printf(string_ptr);       // Display the "World" string

temp = My_struct.fn2;     // Copy func ptr to temp variable 
string_ptr = (*temp)(0);  // Call the HiThere function
printf(string_ptr);       // Display the "Hi" string
string_ptr = (*temp)(1);  // Call the HiThere function
printf(string_ptr);       // Display the "There" string

while(1);     
}


This program is an expansion of the code that I posted in this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=33458
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