From owner-freebsd-hackers Tue Aug 4 15:24:00 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id PAA05979 for freebsd-hackers-outgoing; Tue, 4 Aug 1998 15:24:00 -0700 (PDT) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from elvis.vnet.net (elvis.vnet.net [166.82.1.5]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id PAA05950 for ; Tue, 4 Aug 1998 15:23:48 -0700 (PDT) (envelope-from rivers@dignus.com) Received: from dignus.com (ponds.vnet.net [166.82.177.48]) by elvis.vnet.net (8.8.8/8.8.4) with ESMTP id SAA19257; Tue, 4 Aug 1998 18:23:28 -0400 (EDT) Received: from lakes.dignus.com (lakes [10.0.0.3]) by dignus.com (8.8.8/8.8.5) with ESMTP id SAA03210; Tue, 4 Aug 1998 18:56:20 -0400 (EDT) Received: (from rivers@localhost) by lakes.dignus.com (8.8.8/8.6.9) id SAA16749; Tue, 4 Aug 1998 18:27:34 -0400 (EDT) Date: Tue, 4 Aug 1998 18:27:34 -0400 (EDT) From: Thomas David Rivers Message-Id: <199808042227.SAA16749@lakes.dignus.com> To: chuckr@glue.umd.edu, Nicolas.Souchu@prism.uvsq.fr Subject: Re: C and static initialization with unions Cc: freebsd-hackers@FreeBSD.ORG In-Reply-To: Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Chuck Robey wrote: > On Tue, 4 Aug 1998, Nicolas Souchu wrote: > > > Hi there, > > > > A question about C and static initialization of unions: > > > > suppose, > > > > union foo_t { > > int i; > > char c; > > void *p; > > }; > > > > static union foo_t bar = { (void *)&anyvar }; > > > > The compiler says "warning, making integer from pointer without a cast"... > > Which is true and could lead to bad asm code. > > > > Is there a way to do this properly? Should I forgive unions or what else? > > No, you forgot to mention which union memeber to use. Your foo_t.p > would do nicely. You can't in C initialize only a member of a structure that way. Furthermore, the ANSI C standard specifies that initializations of unions use the first field of the union as the initialization type. Thus, gcc is correct in its message; since the type of the initialization for the union field would be and int, the first field of the union. Now; if you reorder the union fields, so that 'p' is first, you can do the initialization you'd like without any problems. Another posted also responded with the appropriate casts for doing that. Also - you should be aware that ANSI forbids assigning to one element of a union and referencing a different element. (Basically, the 'type-punning' problem) C compilers are allowed to optimize field references based on type; so that: main() { union foo_t foo; void *vp; foo.i = 5; foo.p = 0; if(foo.i) { printf("true\n"); } else { printf("false\n"); } } may, depending on the optimizer, print either true or false. Technically, this is an invalid ANSI C program. - Dave Rivers - > > > > > -- > > Nicolas.Souchu@prism.uvsq.fr > > FreeBSD - Turning PCs into workstations - http://www.FreeBSD.org > > > > To Unsubscribe: send mail to majordomo@FreeBSD.org > > with "unsubscribe freebsd-hackers" in the body of the message > > > > > > ----------------------------+----------------------------------------------- > Chuck Robey | Interests include any kind of voice or data To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message