L
Lauri Alanko
Hello.
I'd like to have a macro NEW(t) that expands to an expression whose
value is a pointer to a newly allocated object of type t that has
automatic storage duration (when used inside a function). If an
explicit initializer is provided, this is easy with a compound
literal:
#define NEWI(t,i) ((t[1]){i})
However, I'm interested in the case where the object is filled in
later in the code, and its initial value is irrelevant. Thus requiring
a dummy initializer feels a bit ugly. The following is illegal, since
an initializer list cannot be empty (why o why?):
#define NEW(t) ((t[1]){})
and the following only works if t is a scalar type:
#define NEW(t) ((t[1]){0})
C always requires an initializer, and there is no universal
initializer. So I'm at my wits' end. Are there any portable solutions
to this?
Thanks,
Lauri
I'd like to have a macro NEW(t) that expands to an expression whose
value is a pointer to a newly allocated object of type t that has
automatic storage duration (when used inside a function). If an
explicit initializer is provided, this is easy with a compound
literal:
#define NEWI(t,i) ((t[1]){i})
However, I'm interested in the case where the object is filled in
later in the code, and its initial value is irrelevant. Thus requiring
a dummy initializer feels a bit ugly. The following is illegal, since
an initializer list cannot be empty (why o why?):
#define NEW(t) ((t[1]){})
and the following only works if t is a scalar type:
#define NEW(t) ((t[1]){0})
C always requires an initializer, and there is no universal
initializer. So I'm at my wits' end. Are there any portable solutions
to this?
Thanks,
Lauri