Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'd go more radical than what people have suggested so far, but maybe it is too much for you. (The 'inline' keyword is C99; you can omit it without much consequence if you code to C89.)</p> <pre><code>/* ** These could be omitted - unless you get still more radical and create ** the format strings at run-time, so you can adapt the %-24s to the ** longest tag you actually have. Plus, with the strings all here, when ** you change the length from 24 to 30, you are less likely to overlook one! */ static const char fmt_int[] = "%-24s [%d]\n"; static const char fmt_long[] = "%-24s [%ld]\n"; static const char fmt_str[] = "%-24s [%s]\n"; /* Plausible extra ... */ static inline void print_long(FILE *fp, const char *tag, long value) { fprintf(fp, fmt_long, tag, value); } static inline void print_int(FILE *fp, const char *tag, int value) { fprintf(fp, fmt_int, tag, value); } static inline void print_str(FILE *fp, const char *tag, const char *value) { fprintf(fp, fmt_str, tag, value); } static void dump_data(FILE *fp, const serial_info_t *info) { dump_long("Starting serial number", info-&gt;start_int_idx); dump_int( "Current Serial number", info-&gt;current_int_idx); /* ... and similar ... */ } </code></pre> <p>Then the calling code would call <code>dump_data()</code> once (with argument <code>stdout</code>) for options 1, 2, 3 and twice (once with <code>stdout</code>, once with file pointer for output file) for option 4.</p> <p>If the number of parameters got truly huge (into the multiple hundreds), I'd even go as far as to consider a data structure which encoded type and offset information (<code>offsetof</code> from <code>&lt;stddef.h&gt;</code>) and pointers to functions and such like, so that there would be just a loop in <code>dump_data()</code> iterating over a structure which encodes all the necessary information.</p> <p>You could also simplify life by using the same basic integer type (<code>long</code> in your example) for all the integer members of the data structure.</p> <p>Fred Brooks in "Mythical Man Month" - a book well worth reading if you've not already done so, but make sure you read the Twentieth Anniversary edition - says in Chapter 9:</p> <blockquote> <p>Show me your flowcharts [code] and conceal your tables [data structures], and I shall continue to be mystified. Show me your tables, and I won't usually need your flowcharts; they'll be obvious.</p> </blockquote> <p>A table-driven version of this code could end up saving space, as well as frustration when having to change a hundred related functions in the same way whereas a simple change in the tabular data could have fixed the whole lot.</p>
 

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