Home arrow Support arrow Forums

Luminary Micro Forums

ckuecker

Platinum Boarder

2008/10/22 13:36

Pointers to functions

Hello,

I am trying to write a simple XML parser. To do this, I am trying to access a function pointer through an array of structures.

I define my structure like this:

Code:

  typedef const struct {     unsigned char count;     void *ptr;                     void *process;     struct PARSE_STRUCT *next; }PARSE_STRUCT;     // // *ptr - pointer to an array of strings to attempt to match to each token found in input string // *process - pointer to array of functions to parse next field after a *ptr match is found. Must  equal number of entries in *ptr, in the same order. May be null if found token is a next-level  opening tag. // *next - pointer to next level PARSE_STRUCT if this is a multi-level statement.



I declare an array of these structures, one for each unique input strung I want to handle:

Code:

  void (*const PS_TYPE_PROD_CONF_LIST[])(char *tagunsigned char arg) = {     &read_mac,            // Function pointers     &read_password }; const unsigned char MSG_TYPE_PROD_CONF[] = {     MSG_XML_PHY_ADDRESS1,  // Strings to match     MSG_XML_HARDWARE_VERS }; PARSE_STRUCT PS_TYPE_PROD_CONF = {     2,    // Number of matches to attempt     (void *)MSG_TYPE_PROD_CONF// Pointer to strings to match     (void *)&PS_TYPE_PROD_CONF_LIST// Pointer to array of value handlers     0 }; PARSE_STRUCT *parse_structures[] = {     &PS_TYPE_PROD_CONF,     &PS_TYPE_WRITE_NET_PROFILES,     &PS_TYPE_WRITE_SERVER_PROFILES };



These all compile correctly. The problem comes when I try to access the desired function through the selected structure. For instance, I want to get to the 'read_password' function from the structure PS_TYPE_PROD_CONF. I can access the process member of the desired structure like this:

Code:

  if(parse_list[i].process// TRUE if pointer exists



How do I access the proper actual function pointer? A statement like this:

Code:

  (parse_list[i].process())(ptrcount);



gives an error 'Error[Pe109]: expression must have (pointer-to-) function type', and I don't see how it could actually access the 'read_password' function rather than only the first element of the array.

I've found plenty of descriptions of how to set up arrays of function pointers, but nothing on using such an array in a structure, let alone accessing it.

Post edited by: ckuecker, at: 2008/10/22 14:19

login or register to reply

hauensts

Senior Boarder

2008/10/23 01:52

Re:Pointers to functions

Hi,

process is currently not a pointer to a function. Replace "void *process;" in PARSE_STRUCT by "void (*process)(char*, unsigned char);" then process is a pointer to a function that returns void and get char* and unsigned char as arguments.

Function call should something like
parse_list[i].process(ptr, count);

I hope this helps.

login or register to reply

ckuecker

Platinum Boarder

2008/10/28 10:25

Re:Pointers to functions

I managed to figure out the proper solution. Thanks to all who commented.

login or register to reply

cb1

Platinum Boarder

2008/10/28 21:07

Re:Pointers to functions

Interesting issue - would you be good enough to share the essence of your solution with your forum? Thanks in advance...

login or register to reply