Is it possible to dynamically define a struct in C

前端 未结 6 1996
暗喜
暗喜 2020-12-14 16:16

I\'m pretty sure this will end up being a really obvious question, and that\'s why I haven\'t found much information on it. Still, I thought it was worth asking :)

B

6条回答
  •  悲哀的现实
    2020-12-14 16:36

    You can't define a source-level struct, but you could do the same thing by setting up a data structure to store a name/tag and offset for each field of the data you want to communicate, and then store/read data at the right offsets according to that. Be sure you align all types to a boundary that's a multiple of sizeof(type) for portability. Of course, unless you're sure the client and server will have the same data representations (endianness and other considerations) and really need the performance of direct access, I would write proper serialize and deserialize routines instead...

提交回复
热议问题