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

pointers to functions
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Trampas
Guest







pointers to functions
PostPosted: Sat Sep 04, 2004 8:38 am     Reply with quote

I read in this post:
http://www.ccsinfo.com/forum/viewtopic.php?t=20133&highlight=pointer+functions

That CCS allows pointers to functions. However I could not get them working, does anyone know if CCS does support pointers to functions?

Thanks
Trampas
bdavis



Joined: 31 May 2004
Posts: 86
Location: Colorado Springs, CO

View user's profile Send private message

PostPosted: Sat Sep 04, 2004 8:50 am     Reply with quote

I read somewhere in the help section that CCS does not support them. Mark replied in the earlier post stating they are supported, so he may have a workaround or maybe they were added since the manual was printed or something... Very Happy
Trampas
Guest







PostPosted: Sat Sep 04, 2004 9:03 am     Reply with quote

Yeap, well I know Mark way toying with the C18 compiler which does support pointer to functions, thus this may be the confusion.

What I am trying to do is create a menu system:

typedef struct {
CHAR name[16];
int (*ptrFunc)();
} MenuItem;

So I create an array of these structures and then pass them to a show menu function, if the user selects the menu it would call the function. This is a really clean way to to do menus and very flexiable.

Maybe someone else has a better method? The only other way I can figured out how to do it is by using a switch case statements, which is not as simple or as clean...

Trampas
bdavis



Joined: 31 May 2004
Posts: 86
Location: Colorado Springs, CO

View user's profile Send private message

PostPosted: Sat Sep 04, 2004 9:18 am     Reply with quote

I'm doing about the same thing - I wanted a struct with a pointer to a function, the string to look for that if found would call the function, and string for a description. It is a clean way to do a command handler or menu since you only have to modify the struct to add new commands. Anyway - I'm using the 18FXXX and couldn't get it to compile, so I went with separate structures and a switch statement for the functions. I replied to Mark's last post on this, so maybe he can show us how it's done. Very Happy
Trampas



Joined: 04 Sep 2004
Posts: 89
Location: NC

View user's profile Send private message MSN Messenger

PostPosted: Sat Sep 04, 2004 2:06 pm     Reply with quote

That is funny! I ran into the same problem for a command structure. I developmed the command line system on a TI DSP and then tried to port to PIC...

BTW check out TI's newer F28xx DSPs they rock! 150MIPS 32bit processor with internal Flash memory, for $15! They are going to be releasing a smaller version in the spring for around $5. However they are 3.3V IO and for somethings the PIC is just more suited.

Trampas
bdavis



Joined: 31 May 2004
Posts: 86
Location: Colorado Springs, CO

View user's profile Send private message

PostPosted: Sat Sep 04, 2004 2:42 pm     Reply with quote

It still amazes me how micro's are getting so cheap, and have so much packed into 'em! They must have a good cache too to run at 150Mips with Flash...
Mark



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

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

PostPosted: Sun Sep 05, 2004 6:10 pm     Reply with quote

http://www.ccsinfo.com/forum/viewtopic.php?t=18345&highlight=function+pointer

Also see "EX_QSORT.C" for another example. I think I read somewhere around 3.160 it was added.

Edit:
3.160 Pointers to functions are now supported
3.160 Examples updated to use more modern chips
3.160 PCW IDE improvments incorporated
3.160 Some COFF debug file problems are resolved
Trampas



Joined: 04 Sep 2004
Posts: 89
Location: NC

View user's profile Send private message MSN Messenger

PostPosted: Tue Sep 07, 2004 8:30 am     Reply with quote

I must be doing something wrong or it does not work:

[code]
typedef int (*pFunc)(void);

typedef struct {
CHAR name[16];
pFunc ptrFunc;
} MenuItem;

UINT one()
{
return 0;
}


MenuItem testMenu[2]={{"one", one}, {"two",0}};


*** Error 27 "...***.c" Line 15(35,36): Expression must evaluate to a constant

That is I get this error where I defince the testMenu array.

Trampas
Trampas



Joined: 04 Sep 2004
Posts: 89
Location: NC

View user's profile Send private message MSN Messenger

PostPosted: Tue Sep 07, 2004 8:34 am     Reply with quote

If I try to declare the pointer to a function the method like K&R states:

Code:

typedef struct {
   CHAR name[16];
   int (*ptrFunc)();
} MenuItem;


Then I get the error:
Quote:
Error 36 "menu.h" Line 22(14,15): Expecting a ; or ,


I guess I should try compiling the qsort example next.

Trampas
Trampas



Joined: 04 Sep 2004
Posts: 89
Location: NC

View user's profile Send private message MSN Messenger

PostPosted: Tue Sep 07, 2004 8:42 am     Reply with quote

I went and compiled the Qsort example and it compiled correctly.

I changed the code to the following:

Code:

void main() {
   int i;
   char c;
   _Cmpfun compare;

   while( TRUE ) {
      do {
        printf("\r\n\r\nSort Assending or Desending (A,D): ");
        c=toupper(getc());
      } while ((c!='A')&&(c!='D'));

      if(c == 'A')
    qsort(data, items, maxchars, assending);
        //compare=assending;
      else
        compare=desending;

      qsort(data, items, maxchars, compare);
      for(i=0; i<items; ++i)
        printf("\r\n%s",data[i]);
   }
}


Which created the errors:

Quote:
*** Error 51 "C:\Program Files\PICC\Examples\EX_QSORT.C" Line 94(31,40): A numeric expression must appear here
*** Error 51 "C:\Program Files\PICC\Examples\EX_QSORT.C" Line 96(7,11): A numeric expression must appear here
2 Errors,


So the answer is that CCS supports pointer to functions sort of... From what I can tell the pointers to functions have to be assigned to a none constant variable, ie variable in data memory. This really sucks as it would be nice to be able to implement menu systems and state machines using pointer to functions in static arrays.

Trampas
Mark



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

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

PostPosted: Tue Sep 07, 2004 9:07 am     Reply with quote

Maybe one of these days they will fully implement it as well as pointers to constant strings! Until then, I'll continue to use the C18 compiler. Here is a snippet of something similar to what you are doing but it does use the C18 compiler.

Code:

typedef rom struct _menu
{
  enum menus      menu;
  enum menus      parentmenu;
  rom const char  *pStr;
  PTR_ROM         *pViewHelp;
  PTR_ROM         *pEditHelp;
  void (*Soft_Key) (signed char index);   /* command function */
  void (*Up_Key) (signed char index);     /* command function */
  void (*Down_Key) (signed char index);   /* command function */
  void (*Left_Key) (signed char index);   /* command function */
  void (*Right_Key) (signed char index);  /* command function */
  void (*Input_Key) (signed char index);  /* command function */
  void (*Relay_Key) (signed char index);  /* command function */
  void (*Day_Key) (signed char index);    /* command function */
  void (*Menu_Key) (signed char index);   /* command function */
  void (*Display_Menu) (void);            /* command function */
}MENU;

MENU  MENU_INPUT =
{
  m_Inputs,               /* MenuID */
  m_Menu,                 /* Parent Menu */
  " INPUTS",              /* Menu Text */
  INPUT_HELP,             /* View Help Text */
  E_INPUT_HELP,           /* Edit Help Text */
  Input_Softkey,          /* Softkey */
  Input_Upkey,            /* Upkey */
  Input_Downkey,          /* Downkey */
  Input_Leftkey,          /* Leftkey */
  Input_Rightkey,         /* Rightkey */
  Input_Inputkey,         /* Inputkey */
  Input_Relaykey,         /* Relaykey */
  NULL,                   /* Daykey */
  Default_Menukey,        /* Menukey */
  DisplayInputMenu        /* Display function */
};
Trampas



Joined: 04 Sep 2004
Posts: 89
Location: NC

View user's profile Send private message MSN Messenger

PostPosted: Tue Sep 07, 2004 9:26 am     Reply with quote

Yeap, maybe it is time to switch to a real compiler...

I would think constant arrays of pointer to functions would be better as the compiler could statically determine execution path.

Trampas
Trampas



Joined: 04 Sep 2004
Posts: 89
Location: NC

View user's profile Send private message MSN Messenger

PostPosted: Tue Sep 07, 2004 1:52 pm     Reply with quote

Yeap I guess you can call me Troll if it makes you feel better.

Trampas
Mark



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

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

PostPosted: Tue Sep 07, 2004 1:56 pm     Reply with quote

Trampas,

I wouldn't waste my time with "Guest". He is obviously not a professional. Everyone is entitled to their own opinion and you certainly have the right to express yours.
Trampas



Joined: 04 Sep 2004
Posts: 89
Location: NC

View user's profile Send private message MSN Messenger

PostPosted: Tue Sep 07, 2004 2:11 pm     Reply with quote

Yeap, I learned a long time ago to leave my feelings at home...

I was thinking about the pointers to function some more and was thinking about trying assigning the pointers at run time..

I will give it a go and get back to you guys.

Trampas
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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