[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[drepper@cygnus.com: Re: element not constant ...]

Hi all,

> What exactly is the problem?

> stdin should always be present before it starts running your code, and
> there's no const up there.

> This should definately not be a problem if it is in any function
> (including main).  If it's global, I really don't see the problem
> there either -- stdin should be initialized beforehand by libc, then
> example gets set to it.

i'll forward you a message from Ulrich Drepper for the
'Initializer element not constant' problem. Hope it helps!

----- Forwarded message from Ulrich Drepper <drepper@cygnus.com> -----

To: Hartmut Koptein <koptein@et-inf.fho-emden.de>
Subject: Re: element not constant ...
From: Ulrich Drepper <drepper@cygnus.com>
Date: 01 Sep 1998 09:40:48 -0700
X-Mailer: Gnus v5.5/XEmacs 20.4 - "Emerald"

Hartmut Koptein <koptein@et-inf.fho-emden.de> writes:

> Hallo Ulrich,
> wie kann man ein globales
> FILE  *fd = stderr;       /* initializer element not constant */

Wieder einmal eine FAQ.

---------------.      drepper at gnu.org  ,-.   1325 Chesapeake Terrace
Ulrich Drepper  \    ,-------------------'   \  Sunnyvale, CA 94089 USA
Cygnus Solutions `--' drepper at cygnus.com   `------------------------

3.9.    I get compiler messages "Initializer element not constant" with
        stdin/stdout/stderr. Why?

{RM,AJ} Constructs like:
static FILE *InPtr = stdin;

lead to this message.  This is correct behaviour with glibc since stdin is
not a constant expression.  Please note that a strict reading of ISO C does
not allow above constructs.

One of the advantages of this is that you can assign to stdin, stdout, and
stderr just like any other global variable (e.g. `stdout = my_stream;'),
which can be very useful with custom streams that you can write with libio
(but beware this is not necessarily portable).  The reason to implement it
this way were versioning problems with the size of the FILE structure.

To fix those programs you've got to initialize the variable at run time.
This can be done, e.g. in main, like:

static FILE *InPtr;
int main(void)
  InPtr = stdin;

or by constructors (beware this is gcc specific):

static FILE *InPtr;
static void inPtr_construct (void) __attribute__((constructor));
static void inPtr_construct (void) { InPtr = stdin; }

----- End forwarded message -----

 Hartmut Koptein                                       EMail:
 Friedrich-van-Senden-Str. 7                           koptein@et-inf.fho-emden.de
 26603 Aurich   
 Tel.: +49-4941-10390                                  koptein@debian.org

Reply to: