You are here

Calling Function from Pointer Inside Struct | Cypress Semiconductor

Calling Function from Pointer Inside Struct

Summary: 2 Replies, Latest post by jeby on 12 Jan 2016 07:00 PM PST
Verified Answers: 0
Last post
Log in to post new comments.
jeby's picture
5 posts


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?

SAMP.CY's picture
Cypress Employee
44 posts

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.


Sampath Selvaraj

jeby's picture
5 posts

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!

Log in to post new comments.