Calling Function from Pointer Inside Struct

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hi,

   

I am working on implementing a terminal menu using the UART user module on a PSOC1 with the IMAGECRAFT compiler and Designer 5.4.  I had already developed the code for this on a PIC18F chip back in college, so naturally dropped it in and hit compile (with fingers crossed of course).  Unfortunately, it didn't work and the compiler is throwing its errors on an instruction that is attempting to call a function from a void pointer that is an element of a structure.

   

 

   

Compiling the attached project gives the error at menu_struct.c line 44 (I have modified the project so that menu_struct.c and menu_struct.h should be the only code in question).  I think that the function pointer needs to be type-cast as a function pointer, which is what I'm trying to do with the (void(*)(void*)).  This syntax makes zero sense to me and I am just regurgitating the syntax from college and that I've found online...

   

 

   

Running into this problem, I did brush up on void pointers, and used knowledge base article Function Pointer - KBA84041 to run some tests.  Based on that, I tried changing line 44 to (*sub_menu_01.menulist[0].item)() which gives an error "found 'void' expected function".  In my mind, this confirms the need for the type-cast.  That actually makes sense the more I think about it, because the compiler has no idea the pointer points to the start of a function.

   

 

   

At this point, I have tried a ton of different combinations of syntax and have googled for hours in search of the correct syntax for this specific situation.  My suspicion at this point is that the syntax for handling void pointers in the PIC MCC18 and IMAGECRAFT compilers is simply different, but at the same time I would have thought this functionality would have been consistent.  The IMAGECRAFT guide that I found only gives some basic information about pointers.  

   

 

   


Does anybody know what the correct syntax for calling a function in this manner?

0 Likes
2 Replies
lock attach
Attachments are accessible only for community members.
SampathS_11
Moderator
Moderator
Moderator
250 sign-ins 250 solutions authored 5 questions asked

Hello Jeby,

   

I am able to get the project compiled by change the last element in struct menulist to

   

void    (*    item)(void);

   

This means change the Line #80 in menu.c to this

   

current_menu = (struct menu*)current_menu->menulist[i-1].item;

   

 

   

Also include typecast of (void *) when initializing the menu member of the struct menulist.

   

Hope this helps. I am attaching the modified project here.

   

Thanks,

   

Sampath Selvaraj

0 Likes
Anonymous
Not applicable

Thanks Sampath!  Your suggestions worked great!  Yeah, that's an interesting solution.  So, if I understand this correctly, the item member of the struct menu was being defined as a void pointer.  When the item type was a menu, the initializations and moving of that pointer address to current_menu didn't require any type-casting.  The problem came when trying to call a function pointer with the item, and I still think it was probably just a syntax issue on my side.

   

By changing the definition of item from a void pointer to a void function pointer, it simplified the syntax to actually call a function from the menu member "item".  But since item is now a void function pointer, initialization of item when it is a menu needs to be cast as a void pointer.  Then when updating current_menu, it needs to be cast as well.  Hope I'm at least close to what's going on here!

   

Thanks again for the help!

0 Likes