Is there a nice way to combine designated initializers from C99, with the result of a malloc?
The following seems to have needless duplication:
typedef struct {
int a, b, c;
} Type;
Type *t = malloc(sizeof *t);
*t = (Type) {
.a = 2,
.b = 3,
.c = 5,
};
Can the use of Type, and *t be removed from the above code?
Since you asked 😉 there is one tool in C to avoid explicit duplication of code, macros. That said I don’t see a way not to repeat at least the name of the type. But in C++ they can’t either, so C is at least as good 🙂
The easiest I see is
which would give
this has several advantages.
standard representations of the
0for float types or pointers.NB: Observe the
constin the macro, this allows several instances of the compound literal to be folded, if the compiler decides this to be relevant. Also there are means to have a variant where the list of designators is optional, see P99 below.The disadvantage is the
memcpyand I would be happier with an assignment. Second there is no check for failure ofmallocbefore using the result, but one could probably come across with some weirdness to have the code exit nicely.In P99 I go a slightly different way. There we always have an initialization function for a type, something like
which by macro magic can be made to provide default arguments for
a,bandcif they are omitted. Then you can simply use something likein your application code. This is better, since it avoids dereferrencing the pointer when the call to
mallocfailed. On the other hand this reintroduces an order to the initializers, so not perfect either.