D
David Resnick
I'm faced with a header with anonymous structures declared inside a
union like this:
union msg {
struct {
int a;
int b;
} s1;
struct {
char c;
double d;
double e;
} s2;
};
I want to apply the sizeof operator to one of these structs.
Changing the header is not currently an option, alas.
I currently can see two ways to do this as shown in this test program:
#include <stdio.h>
/* above union declaration here */
int main(void)
{
union msg foo;
printf("%lu\n", (unsigned long)sizeof(foo.s1));
printf("%lu\n", (unsigned long)sizeof(((union msg*)(NULL))->s2));
return 0;
}
Is the second legitimate? It compiles without complaint, but looks
dodgy.
I hate to create a fake instance of the union just to apply the sizeof
operator.
Or is there another way?
Thanks,
-David
union like this:
union msg {
struct {
int a;
int b;
} s1;
struct {
char c;
double d;
double e;
} s2;
};
I want to apply the sizeof operator to one of these structs.
Changing the header is not currently an option, alas.
I currently can see two ways to do this as shown in this test program:
#include <stdio.h>
/* above union declaration here */
int main(void)
{
union msg foo;
printf("%lu\n", (unsigned long)sizeof(foo.s1));
printf("%lu\n", (unsigned long)sizeof(((union msg*)(NULL))->s2));
return 0;
}
Is the second legitimate? It compiles without complaint, but looks
dodgy.
I hate to create a fake instance of the union just to apply the sizeof
operator.
Or is there another way?
Thanks,
-David