L
Lauri Alanko
Given:
typedef struct { int foo; } Bar;
The following is legal (N869 6.7.2.1#12):
Bar b = { 42 };
int* ip = &bar.foo;
Bar* bp = (Bar*) ip;
assert(bp->foo == 42);
But this, to my understanding, might not be:
int i = 42;
int* ip = &i;
Bar* bp = (Bar*) ip;
assert(bp->foo == 42);
The only practical problem I can think of here is that Bar and int
might have different alignments for some reason.
However, all structs are guaranteed to have the same alignment
(6.2.5#27), so how about the following?
typedef struct { Bar bar; } Baz;
Bar b = { 42 };
Baz* zp = (Baz*) &b;
assert(zp->bar.foo == 42);
At this point, although I'm guessing this isn't strictly conforming, I
can't think of any real-world reason why this wouldn't work.
Anyone?
Lauri
typedef struct { int foo; } Bar;
The following is legal (N869 6.7.2.1#12):
Bar b = { 42 };
int* ip = &bar.foo;
Bar* bp = (Bar*) ip;
assert(bp->foo == 42);
But this, to my understanding, might not be:
int i = 42;
int* ip = &i;
Bar* bp = (Bar*) ip;
assert(bp->foo == 42);
The only practical problem I can think of here is that Bar and int
might have different alignments for some reason.
However, all structs are guaranteed to have the same alignment
(6.2.5#27), so how about the following?
typedef struct { Bar bar; } Baz;
Bar b = { 42 };
Baz* zp = (Baz*) &b;
assert(zp->bar.foo == 42);
At this point, although I'm guessing this isn't strictly conforming, I
can't think of any real-world reason why this wouldn't work.
Anyone?
Lauri