From owner-freebsd-hackers Tue Aug 4 17:52:20 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id RAA01719 for freebsd-hackers-outgoing; Tue, 4 Aug 1998 17:52:20 -0700 (PDT) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from smtp02.primenet.com (smtp02.primenet.com [206.165.6.132]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id RAA01713 for ; Tue, 4 Aug 1998 17:52:17 -0700 (PDT) (envelope-from tlambert@usr02.primenet.com) Received: (from daemon@localhost) by smtp02.primenet.com (8.8.8/8.8.8) id RAA09936; Tue, 4 Aug 1998 17:52:06 -0700 (MST) Received: from usr02.primenet.com(206.165.6.202) via SMTP by smtp02.primenet.com, id smtpd009909; Tue Aug 4 17:52:02 1998 Received: (from tlambert@localhost) by usr02.primenet.com (8.8.5/8.8.5) id RAA26979; Tue, 4 Aug 1998 17:52:01 -0700 (MST) From: Terry Lambert Message-Id: <199808050052.RAA26979@usr02.primenet.com> Subject: Re: C and static initialization with unions To: Nicolas.Souchu@prism.uvsq.fr (Nicolas Souchu) Date: Wed, 5 Aug 1998 00:52:01 +0000 (GMT) Cc: freebsd-hackers@FreeBSD.ORG In-Reply-To: <19980804185938.36803@breizh.prism.uvsq.fr> from "Nicolas Souchu" at Aug 4, 98 06:59:38 pm X-Mailer: ELM [version 2.4 PL25] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG > 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? Put the largest possible type first, and cast to that. The real answer is that GCC is stupid about agregate initializations, and doesn't store more than one symbol type for the assign. Logically, one would expect: static union foo_t bar.p = { (void *)&anyvar }; to work, since the error for the code without an anyvar definition is: initializer element for `bar.i' is not constant So it "knows" about "bar.i" distinct from "bar". PS: Should you be posting this to comp.lang.c, or better yeat, reading the FAQ from comp.lang.c? Terry Lambert terry@lambert.org --- Any opinions in this posting are my own and not those of my present or previous employers. To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message