You could perhaps customize the GCC compiler (e.g. with the MELT domain specific language to extend GCC) to add some new __attribute__
which would generate the printing code from the types (probably annotated). But such an approach would take several days of work, and I won't recommend it in general, unless you have a really big program to work on.
Notice that your problem is more complex than what you think at first: a pointer inside a struct
may have to be printed and followed (or to be left out, if it is only for house-keeping). And complex data structures have shared components so you need to manage the set of already visited data. Notice also that printing union
-s is in general not possible, unless you have some discriminator (and conventions about it, which are outside of the C declarations).
I would rather use (or develop) a tool which accepts the description of your data structures (with additional annotation) and output printing routines for it, and the C declarations of your data.
BTW, your problem is related to serialization