N
Nate Eldredge
Consider the following pseudo-code:
#include <opaque.h>
struct foo {
int a;
opaque_t op;
int b;
};
struct foo blah = { 17, /* ??? */ , 23 };
Here we suppose that `opaque_t' is defined in <opaque.h> as some type
not known to us, or which we cannot rely on. In particular, it may be
a scalar or compound type. The goal is to initialize `blah' in such a
way that `blah.a == 17' and `blah.b == 23'. We don't care about the
value of `blah.op'.
It seems to me that there is no way to do this in standard C. Is this
correct, or am I missing something?
A workaround would be to rearrange the members of `struct foo' as
struct foo2 {
int a;
int b;
opaque_t op;
};
struct foo2 blah2 = { 17, 23 };
I believe this is legal, but I could be wrong. My compiler warns
about a missing initializer, but accepts the code.
If `opaque_t' is known to be a compound type, we could write
struct foo blah3 = { 17, { }, 23 };
which again provokes a missing initializer warning but is accepted.
Again I am curious whether it is actually legal.
I thought of this issue after reading a post here a couple weeks ago,
"Exception handling crashes or exits" by Fabiano Sidler,
<[email protected]>, in which the poster wants a struct
with a jmp_buf as one member. He initializes that member with { 0 },
which is certainly non-portable. But I wondered if there is a
portable way to do the same thing.
If not, it seems like it might be useful for the authors of <opaque.h>
to provide a macro which is suitable as an initializer for opaque_t.
E.g. <opaque.h> might contain
typedef opaque_t struct {
int x,
double d;
};
#define OPAQUE_INITIALIZER { 0, 0.0 }
so that one could write
struct foo blah = { 17, OPAQUE_INITIALIZER, 23 };
In particular, this could be a useful extension for library authors to
provide for opaque library types (e.g. jmp_buf, FILE, etc).
Any thoughts?
#include <opaque.h>
struct foo {
int a;
opaque_t op;
int b;
};
struct foo blah = { 17, /* ??? */ , 23 };
Here we suppose that `opaque_t' is defined in <opaque.h> as some type
not known to us, or which we cannot rely on. In particular, it may be
a scalar or compound type. The goal is to initialize `blah' in such a
way that `blah.a == 17' and `blah.b == 23'. We don't care about the
value of `blah.op'.
It seems to me that there is no way to do this in standard C. Is this
correct, or am I missing something?
A workaround would be to rearrange the members of `struct foo' as
struct foo2 {
int a;
int b;
opaque_t op;
};
struct foo2 blah2 = { 17, 23 };
I believe this is legal, but I could be wrong. My compiler warns
about a missing initializer, but accepts the code.
If `opaque_t' is known to be a compound type, we could write
struct foo blah3 = { 17, { }, 23 };
which again provokes a missing initializer warning but is accepted.
Again I am curious whether it is actually legal.
I thought of this issue after reading a post here a couple weeks ago,
"Exception handling crashes or exits" by Fabiano Sidler,
<[email protected]>, in which the poster wants a struct
with a jmp_buf as one member. He initializes that member with { 0 },
which is certainly non-portable. But I wondered if there is a
portable way to do the same thing.
If not, it seems like it might be useful for the authors of <opaque.h>
to provide a macro which is suitable as an initializer for opaque_t.
E.g. <opaque.h> might contain
typedef opaque_t struct {
int x,
double d;
};
#define OPAQUE_INITIALIZER { 0, 0.0 }
so that one could write
struct foo blah = { 17, OPAQUE_INITIALIZER, 23 };
In particular, this could be a useful extension for library authors to
provide for opaque library types (e.g. jmp_buf, FILE, etc).
Any thoughts?