2020-11-29

Casting a struct onto variable length buffer

Is it possible to create struct that I can cast to a buffer of variable size? For example if I have some data that looks something like this:

0  1  2  3  4  5  6  7  8
+--+--+--+--+--+--+--+--+
|                       |
/                       /
/          DATA         /
|                       |
+--+--+--+--+--+--+--+--+
|          NAME         |
+--+--+--+--+--+--+--+--+
|          TIME         |
+--+--+--+--+--+--+--+--+
|          ETC          |
+--+--+--+--+--+--+--+--+

Would it be possible to have a struct similar to this

typedef struct{
    uint8_t data[];
    uint8_t name;
    uint8_t time;
    uint8_t etc;
}Struct;

and then resize Struct.data at runtime to I can cast the structure directly onto the buffer?

I know I could just make a struct that uses pointers and have them point to parts of the buffer, but I was curious if this is possible because it would simplify my code and make it easier to read/use.



from Recent Questions - Stack Overflow https://ift.tt/3o40xAh
https://ift.tt/eA8V8J

No comments:

Post a Comment