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

loading structs with pointers to function

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



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

loading structs with pointers to function
PostPosted: Sat Sep 16, 2006 5:10 pm     Reply with quote

Hi,

I am trying to implement a basic menu system that I think it should work, but does not.

This is the most basic code that shows the problem.

Code:
#include <18f452.h>
   #device adc=8
   #use      delay( clock=40000000 )
   #fuses    H4, NOWDT, PUT, NOLVP//, CPD, PROTECT, CPB, WRT, WRTC, BROWNOUT
   #use      rs232( baud=19200, parity = n, xmit = pin_c6, rcv = pin_c7, bits = 8 )
   #zero_ram
   #use rtos( timer=3, minor_cycle=5ms )
   #case

char b1, b2, menu;
typedef void (*function_ptr)();

void inc_b1() { b1++; }
void dec_b1() { b1--; }
void inc_b2() { b2++; }
void dec_b2() { b2--; }
void menu1() { printf( "\n\rMenu #1  b1=%03u", b1 ); }
void menu2() { printf( "\n\rMenu #2  b2=%03u", b2 ); }

struct
{
    function_ptr menu_routine;
    char menu_previous;
    char menu_next;
    function_ptr inc_routine;
    function_ptr dec_routine;
}
main_screen_config[2] = { { menu1, 0, 1, inc_b1, dec_b1 } ,
                          { menu2, 1, 2, inc_b2, dec_b2 } };

main()
{
   function_ptr test;

   menu=0;

   test = menu1; // here test has the correct address
   (*test)(); //call function

   test = main_screen_config[menu].inc_routine; // test becomes NULL here
   test = main_screen_config[0].inc_routine; // test is still NULL

   printf( "b1 value= %u", b1 );
}


The pointer 'test' can not load the correct function address from the struct, or the struct is not being loaded at all...

Compiler version is 249.

Thank you in advance for any suggestions.
Ttelmah
Guest







PostPosted: Sun Sep 17, 2006 3:29 am     Reply with quote

The problem is that you cannot initialise a structure during compile time, with the function addresses. You will find the structure entries contain 0' for each pointer. You need to manually initialise the structure at the start of the program with the addresses to use. Also, the compiler doesn't actually 'instance' the pointer, till it 'sees' it as having been accessed. Unfortunately writing it to a structure, doesn't seem to trigger this...
So, (to allow the other values to be initialised during compiler time), change the pointer to an int16, and use something (bulky), like:

Code:

struct
{
    int16 menu_routine;
    char menu_previous;
    char menu_next;
    int16 inc_routine;
    int16 dec_routine;
}
main_screen_config[2] = { { 0, 0, 1, 0, 0 } ,
                          { 0, 1, 2, 0, 0 } };

main()
{
   function_ptr test;

   menu=0;
   test=menu1;
   main_screen_config[0].menu_routine=test;
   test=inc_b1;
   main_screen_config[0].inc_routine=test;
   test=dec_b1;
   main_screen_config[0].dec_routine=test;
   test=menu2;
   main_screen_config[1].menu_routine=test;
   test=inc_b2;
   main_screen_config[1].inc_routine=test;
   test=dec_b2;
   main_screen_config[1].dec_routine=test;
   test = menu1; // here test has the correct address
   (*test)(); //call function

   test = main_screen_config[menu].inc_routine; 
   test = main_screen_config[0].inc_routine;

The 'stupid' thing, is that whatever type you declare the structure variable to be, it'll complain about an 'invlaid type conversion', if you try to write the address to it, yet if you go 'via' a temporary variable, it'll accept it (and work...).

Best Wishes
future



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

PostPosted: Sun Sep 17, 2006 1:09 pm     Reply with quote

This is bad... The code will look ugly as the menu size grows up.

I was hoping to have up to like 30 menus.

I remember seeing arrays initialized like this on the forum... maybe I could put all the menu structure in rom and copy to ram struct at runtime.

Thank you.
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