Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One effective way I have seen is to use macros (an often hated tool). Macros have two features that can help make life much easier, the single hash '#' which can turn a parameter into a string, i.e. <code>#_name</code> will be translated into <code>"fieldName1"</code> and the double hash '##' which concatenate parameters with other things which in turn can expand new things, i.e. <code>STRUCT_##_type##_str</code> that will be translated into <code>STRUCT_int_str</code> which will be translated into <code>"%d"</code> </p> <p>First wrap the structure or "description" in macros i.e. into an own file (the-struct.def)</p> <pre><code>STRUCT_BEGIN(s_my_struct) STRUCT_FIELD(int, fieldName1) STRUCT_FIELD(int, fieldName2) STRUCT_FIELD(int, fieldName3) STRUCT_END(s_my_struct) // Note that you can add more structs here and all will automatically get defined and get the print function implemented </code></pre> <p>Then one can define the macros differently in places where one want to declare or implement things that should handle the struct. i.e.</p> <pre><code>#define STRUCT_BEGIN(_name) struct _name { #define STRUCT_END(_name) }; #define STRUCT_FIELD(_type, _name) _type _name; #include "the-struct.def" // then undef them #undef STRUCT_BEGIN #undef STRUCT_END #undef STRUCT_FIELD </code></pre> <p>and to make a function that prints the struct</p> <pre><code>#define STRUCT_BEGIN(_name) void print_ ## _name(struct _name *s) { #define STRUCT_END(_name) } #define STRUCT_FIELD(_type, _name) printf("%s = " STRUCT_##_type##_str "\n", #_name, s-&gt;_name); #define STRUCT_int_str "%d" /* this is to output an int */ // add more types... #include "the-struct.def" // then undef them #undef STRUCT_BEGIN #undef STRUCT_END #undef STRUCT_FIELD #undef STRUCT_int_str </code></pre> <p>other uses can be to auto-generate functions to ie swap bytes etc.</p> <p>Made a small example as a gist here <a href="https://gist.github.com/3786323" rel="nofollow">https://gist.github.com/3786323</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload