0

For a project, I need to store an array of structures in PROGMEM. Array MessageTable[] will be composed of about ten struct InMessage elements. InMessage looks sort of like this:

struct AvcInMessage {
  MessageHandler  msgHandler;
  byte            dataSize;
  byte            data[8];
  char            description[20];
};

MessageHandler is not a data type... yet. I'm not really sure what data type to use. I want to be able to pick a message from the table and then call MessageTable[x].msgHandler() if that makes sense. I'm almost positive that this involves a pointer. Does someone here have an idea about what I'm looking for?

  • It might be I'm misunderstanding, but it looks like you want to store in PROGMEM an array of structs containing a reference to an object... That looks a little overkill: do you need to have different message handlers for each item in the array? – Roberto Lo Giacco Dec 10 '18 at 2:04
3
typedef void (*MessageHandler)(void);   // def messagehandler as pointer to func

struct AvcInMessage {
  MessageHandler  msgHandler;
  byte            dataSize;
  byte            data[8];
  char            description[20];
} InMessage[] = {
   {
      function1, <datasize>, {<byte0>, <byte1>, ..., }, "DescriptionHere"
   },
   // etc, for however many elements InMessage[] has.
};

The typedef defines a "pointer to function of void returning void" - fix up the argument count and types, and the return type to your needs. We're assuming each element's function has the same kind of arglist and return type. (Update: Sorry for the bug in my first iteration).

The next statement both defines the array of struct and initializes N of them. Provide an element count in the [] if you don't have a full set of initializers at compile time.

  • I think I understand what you mean, but it doesn't compile. I get expected unqualified-id before ')' token. – Bo Thompson Dec 10 '18 at 2:24
  • 2
    should that be typedef void (*MessageHandler)(void); – Jaromanda X Dec 10 '18 at 4:26
  • @JaromandaX has it (thank you!). I've updated my answer. – JRobert Dec 10 '18 at 13:06
  • Confirming that the revised code works. Thank you very much, both. – Bo Thompson Dec 13 '18 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.