Here is my problem, i have several simmilar structue definitions (containers for messages)
struct FIRST{ unsigned char Word_Pos[8] unsigned char Data[8] unsigned char Msg_Len }; struct FIRST A
struct SECOND{ unsigned char Word_Pos[4] unsigned char Data[4] unsigned char Msg_Len }; struct SECOND B
struct THIRD{ unsigned char Word_Pos[10] unsigned char Data[10] unsigned char Msg_Len }; struct THIRD C
Is there a way to declare single pointer (lets say "P") such that i will be able to point to each of these structures so i can transparently use: P->Word_Pos [] P->Data [] P->MSG_Len in my ISR.
I realise i could just declare 3 structures of type THIRD and just use one pointer easily, but in my actual application there will be many of these simmilar but different structures and i am trying to save ram.
How about a union?
I think union is what you are looking for. Void pointers can also be used for this. Unions are better since they clarify the intention of the programmer at declaration level.
...might it not be better to be agnostic to the message type, simply working with putting raw bytes to/from a buffer - and leave the message packing/unpacking to the main application...?
A union is not too fun if the pointer is to an entry with 8 of each entry but used as if the pointer was to an entry with 4 of each entry.
I would probably write accessor functions to handle the data.
But it would probably be even better to analyze the needs and see if the data structures can be rewritten in a better way, so that you get something like:
struct { uint8_t len; struct { uint8_t data; uint16_t word; } elements[SIZE]; };
Now a generic function can use a pointer and all the function needs to know is how many elements that are safe to access. And if "len" stores that value, then the pointer can be sent all around the program without problems.
You are absolutely right. I will most likely create a union of type unsigned char[] and my structure.
BTW not that it makes much difference but Data was supposed to be an unsigned int not char. I wish it was as easy as a sequential buffer read though. When transmitted each word needs to be byte swapped. There can be up to 100 two byte words however we will never modifying more than 8 words in any message. All unmodified words need a 0x00 transmitted as a placeholder
I guess now the uses of Word_Pos becoms more apparent...
I like this, I will give it a try before I use a union. I think it would be a cleaner approach. Thank you.