Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > accessing comon initial sequence in union

Reply
Thread Tools

accessing comon initial sequence in union

 
 
S.Tobias
Guest
Posts: n/a
 
      08-12-2004
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.

--
Stan Tobias
sed 's/[A-Z]//g' to email
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to iterate over sequence and non-sequence ? stef mientki Python 13 10-20-2007 10:21 AM
unions with structures of common initial sequence aarklon@gmail.com C Programming 1 04-01-2006 01:24 AM
union in struct without union name Peter Dunker C Programming 2 04-26-2004 07:23 PM
map XML union to C union (and vice-versa) Matt Garman XML 1 04-25-2004 12:40 AM
BOOT SEQUENCE (how to change boot sequence) bird Computer Support 13 12-24-2003 02:20 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57