S
S.Tobias
Quote from 6.5.2.3 Structure and union members, Examle 3:
The following is not a valid fragment (because the union
type is not visible within function f):
struct t1 { int m; };
struct t2 { int m; };
int f(struct t1 * p1, struct t2 * p2)
{
if (p1->m < 0)
p2->m = -p2->m;
return p1->m;
}
int g()
{
union {
struct t1 s1;
struct t2 s2;
} u;
/* ... */
return f(&u.s1, &u.s2);
}
Would the code become valid if we expanded function f inside g?
struct t1 { int m; };
struct t2 { int m; };
int g()
{
union {
struct t1 s1;
struct t2 s2;
} u;
struct t1 * p1;
struct t2 * p2;
/* ... */
p1 = &u.s1;
p2 = &u.s2;
if (p1->m < 0)
p2->m = -p2->m;
return p1->m;
}
This seems to satisfy 6.5.2.3#5 ("One special guarantee..."): the union
declaration is visible throughout the function g; but I feel this code
is still wrong.
The following is not a valid fragment (because the union
type is not visible within function f):
struct t1 { int m; };
struct t2 { int m; };
int f(struct t1 * p1, struct t2 * p2)
{
if (p1->m < 0)
p2->m = -p2->m;
return p1->m;
}
int g()
{
union {
struct t1 s1;
struct t2 s2;
} u;
/* ... */
return f(&u.s1, &u.s2);
}
Would the code become valid if we expanded function f inside g?
struct t1 { int m; };
struct t2 { int m; };
int g()
{
union {
struct t1 s1;
struct t2 s2;
} u;
struct t1 * p1;
struct t2 * p2;
/* ... */
p1 = &u.s1;
p2 = &u.s2;
if (p1->m < 0)
p2->m = -p2->m;
return p1->m;
}
This seems to satisfy 6.5.2.3#5 ("One special guarantee..."): the union
declaration is visible throughout the function g; but I feel this code
is still wrong.