public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* FILE *foo = stderr; ==>`initializer element is not constant'
@ 2005-01-28 15:11 Richie Baldwin
  2005-01-28 15:49 ` Ian Lance Taylor
  0 siblings, 1 reply; 3+ messages in thread
From: Richie Baldwin @ 2005-01-28 15:11 UTC (permalink / raw)
  To: gcc-help

When trying to compile a c file using stderr, I have the declaration:
FILE *foo = stderr;
and it keeps giving me "Initializer element is not constant". I was 
wondering what could be done to preven this error from happening.   I'm 
not sure if this is incorrect code or what, but any help would be 
appreciated.

The file is a short one, it is:
#include <stdio.h>
FILE *foo = stderr;

and it gives me the error on line 2.

Any assistance that you all could provide would be greatly appreciated.
Richard Baldwin.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: FILE *foo = stderr; ==>`initializer element is not constant'
  2005-01-28 15:11 FILE *foo = stderr; ==>`initializer element is not constant' Richie Baldwin
@ 2005-01-28 15:49 ` Ian Lance Taylor
  0 siblings, 0 replies; 3+ messages in thread
From: Ian Lance Taylor @ 2005-01-28 15:49 UTC (permalink / raw)
  To: Richie Baldwin; +Cc: gcc-help

Richie Baldwin <baldwinr@monolithss.com> writes:

> When trying to compile a c file using stderr, I have the declaration:
> FILE *foo = stderr;
> and it keeps giving me "Initializer element is not constant". I was
> wondering what could be done to preven this error from happening.
> I'm not sure if this is incorrect code or what, but any help would be
> appreciated.
> 
> The file is a short one, it is:
> #include <stdio.h>
> FILE *foo = stderr;
> 
> and it gives me the error on line 2.
> 
> Any assistance that you all could provide would be greatly appreciated.
> Richard Baldwin.

This is an issue with the system library, not with the compiler.  The
<stdio.h> file provided by the system library is providing a
definition of stderr which is not a constant.  This is permitted by
the C standard, so your program is not highly portable.  You neglected
to see which system you are using, so it is difficult to say anything
more precise.

Ian

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: FILE *foo = stderr; ==>`initializer element is not constant'
@ 2005-01-29  8:38 Danny Smith
  0 siblings, 0 replies; 3+ messages in thread
From: Danny Smith @ 2005-01-29  8:38 UTC (permalink / raw)
  To: baldwinr; +Cc: gcc-help

Richie Baldwin  wrote:
> When trying to compile a c file using stderr, I have the declaration:
> FILE *foo = stderr;
> and it keeps giving me "Initializer element is not constant".

This will happen on windows platfrom because stderr is imported from a dll
library,

Effectively this means:

extern FILE (*_imp___iob)[]; /* A pointer to an array of FILE */
#define _iob (*_imp___iob) /* An array of FILE */
#define stderr (&_iob[STDERR_FILENO])

so stderr is indeed not known at compile time.

A workaround is to use an init function:
#include <stdio.h>

FILE * foo;

void intit_foo()
{
  foo = stderr;
}

int main()
{
  init_foo();
 ...
}

Danny

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-01-29  8:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-28 15:11 FILE *foo = stderr; ==>`initializer element is not constant' Richie Baldwin
2005-01-28 15:49 ` Ian Lance Taylor
2005-01-29  8:38 Danny Smith

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).