Question for the C heads here.
Is there a more elegant approach to parsing and creating messages of a specific format without doing a bunch of super error prone math / manual byte fiddling in an array? /1
Conversation
Replying to
Should be able to cast to struct pointers to simplify. Just make sure C stores your struct in memory as you expect. You can also switch on message type to handle specific message types, cast further, etc. If I understand the problem correctly.
1
I've stumbled upon this right now. I feel a bit uneasy about it as that seems non-standard and I have no control over the compiler being used by the user. Having it portable would be important.
2
I've been using macro composition for this. This might be a bit dense of an explanation, but define your struct as a macro #define MY_STRUCT(field) field(name1,type1) field(name2,type2) and then create a macro SERIALIZE that you can instantiate as MY_STRUCT(SERIALIZE).
1
2
You can the define a macro STRUCT that creates the C struct when you do MY_STRUCT(STRUCT). It's a straightforward way to get "type class instances" in C.



