2 ms·
Don't use C99 initializers, or not directly. Write a macro: struct point { double x, y; } #define init_point(x, y) { (x), (y) } /* C90 */ #define
by kazinator 8y ago
Don't use C99 initializers, or not directly. Write a macro:
struct point {
double x, y;
}
#define init_point(x, y) { (x), (y) } /* C90 */
#define init_point(x, y) { .x = (x), .y = (y) } // C99
struct point p = init_point(0.5, 1.5);
The macro is superior because macro calls are checked for number of arguments. If you have a structure in which certain members must be initialized (to a non-default value), and such members can be added over time, that will save your butt. How? Because when you add a new member that must not be zero/null, you add a parameter to the macro. Then, the compiler will tell you all the places where the macro is called with too few parameters.
Designated initializers eliminate some of the error-prone aspects of C90 style, but don't help enforce the discipline of initializing all members, or a certain subset of members.
Functional abstraction with checked arguments beats fancy new syntax.