Using stderr in an initialization?
Steve Kargl
sgk at troutmask.apl.washington.edu
Fri May 2 20:57:20 UTC 2008
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 at 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
More information about the freebsd-ports
mailing list