accessing comon initial sequence in union

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,088
Latest member
JeremyMedl

Latest Threads

Top