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.
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.
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.
I like this, I will give it a try before I use a union. I think it would be a cleaner approach. Thank you.
View all questions in Keil forum