Date: Wed, 5 Aug 1998 00:52:01 +0000 (GMT) From: Terry Lambert <tlambert@primenet.com> To: Nicolas.Souchu@prism.uvsq.fr (Nicolas Souchu) Cc: freebsd-hackers@FreeBSD.ORG Subject: Re: C and static initialization with unions Message-ID: <199808050052.RAA26979@usr02.primenet.com> In-Reply-To: <19980804185938.36803@breizh.prism.uvsq.fr> from "Nicolas Souchu" at Aug 4, 98 06:59:38 pm
next in thread | previous in thread | raw e-mail | index | archive | help
> 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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199808050052.RAA26979>