Date: Fri, 2 May 2008 13:53:51 -0700 From: Steve Kargl <sgk@troutmask.apl.washington.edu> To: Bakul Shah <bakul@bitblocks.com> Cc: freebsd-ports@freebsd.org Subject: Re: Using stderr in an initialization? Message-ID: <20080502205351.GA67671@troutmask.apl.washington.edu> In-Reply-To: <20080502204355.6349B5B3B@mail.bitblocks.com> References: <20080502202356.GA67129@troutmask.apl.washington.edu> <20080502204355.6349B5B3B@mail.bitblocks.com>
next in thread | previous in thread | raw e-mail | index | archive | help
On Fri, May 02, 2008 at 01:43:55PM -0700, Bakul Shah wrote: > On Fri, 02 May 2008 13:23:56 PDT Steve Kargl <sgk@troutmask.apl.washington.edu> wrote: > > I'm porting a piece of code to FreeBSD, and I've run into > > a problem that I currently don't know how to solve. I scanned > > both the Porter's Handbook and the Developer's Handbook, but > > came up empty. > > > > A reduce testcase is > > > > #include <stdio.h> > > > > typedef FILE *FILEP; > > > > static FILEP outfile = {stderr}; > > > ... > > GCC gives > > > > troutmask:sgk[204] cc -o z a.c > > a.c:5: error: initializer element is not constant > > a.c:5: error: (near initialization for 'outfile') > > > > So, anyone have a > > suggestion on how to change line 5 to satisfy gcc? > > It *used* to be the case that stderr was a macro referring to > something like &_iob[2] which is a link time constant > expression. As per section 7.19.1 in the C standard, the > stderr macro is an expression of type `pointer to file' but > not a constant. You wouldn't expect the following to work, > would you? > > FILE* f; > FILE* outfile = f; > > It is the exact same thing. But you can do > > static FILE** _outfile = &stderr; > #define outfile (*_outfile) > > to achive the effect you want. Thanks for the suggestion. This is K&R era code, and if I read the dates in comments correctly, it predates the formation of the ANSI C standard committee by more than 4 years. -- Steve
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20080502205351.GA67671>