Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 02 May 2008 13:43:55 -0700
From:      Bakul Shah <bakul@bitblocks.com>
To:        Steve Kargl <sgk@troutmask.apl.washington.edu>
Cc:        freebsd-ports@freebsd.org
Subject:   Re: Using stderr in an initialization? 
Message-ID:  <20080502204355.6349B5B3B@mail.bitblocks.com>
In-Reply-To: Your message of "Fri, 02 May 2008 13:23:56 PDT." <20080502202356.GA67129@troutmask.apl.washington.edu> 

next in thread | previous in thread | raw e-mail | index | archive | help
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.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20080502204355.6349B5B3B>